0
\$\begingroup\$

I am trying to make a pong game in a 3D landscape. I want to make the ball bounce around the box or the Pong pad. But the ball just goes through the walls and pad, but the pad collides with the walls.

Ball H File

// Fill out your copyright notice in the Description page of Project Settings.

pragma once

include "CoreMinimal.h"

include "GameFramework/Actor.h"

include "Ball.generated.h"

UCLASS() class PONG_API ABall : public AActor { GENERATED_BODY()

public: // Sets default values for this actor's properties ABall();

int dx, dy;

protected: // Called when the game starts or when spawned virtual void BeginPlay() override;

UPROPERTY(EditAnywhere, Category = "Components") UStaticMeshComponent* BallMesh;

UPROPERTY(EditAnywhere, Category = "Movement") class USceneComponent* BallMovement;

UFUNCTION() void Bounce(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit);

public: // Called every frame virtual void Tick(float DeltaTime) override; };

Ball CPP File

// Fill out your copyright notice in the Description page of Project Settings.

include "Ball.h"

include "Components/StaticMeshComponent.h"

include "Components/SceneComponent.h"

include "Wall.h"

include "FrontWall.h"

include "BackWall.h"

// Sets default values ABall::ABall() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true;

BallMesh = CreateDefaultSubobject<UStaticMeshComponent>("BallMesh");

BallMovement = CreateDefaultSubobject<USceneComponent>("BallMovement");

SetRootComponent(BallMesh);

OnActorHit.AddDynamic(this, &ABall::Bounce);

dx = -5.f; dy = -10.f; }

// Called when the game starts or when spawned void ABall::BeginPlay() { Super::BeginPlay();

}

void ABall::Bounce(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit) { UE_LOG(LogTemp, Warning, TEXT("Hey!!!")); if (AWall* W = Cast<AWall>(OtherActor)) { dx = -1; } else { if (ABackWall BW = Cast<ABackWall>(OtherActor)) { UE_LOG(LogTemp, Warning, TEXT("You have lost a point")); } dy *= -1; } }

// Called every frame void ABall::Tick(float DeltaTime) { Super::Tick(DeltaTime);

FVector MoveDelta = FVector(dx, dy, 0.f); AddActorWorldOffset(MoveDelta); }

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.