Hi! I am currently trying to recreate Atari Pong in UE 5.03, and I cannot for the life of me get the player paddle to collide with the bounds of the game (top and bottom wall). I followed this documentation almost exactly. I do not use the Camera Component, Spring Arm Component, or any of the particle components. Instead of a USphereComponent, I use a UBoxComponent. I additionally only use the functionality for MoveForward (which is my paddle moving on the X axis).
Attached below is my code.
Pawn Header File
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Paddle_Player1.generated.h"
UCLASS()
class PONGCLONE_API APaddle_Player1 : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APaddle_Player1();
UPROPERTY(EditAnywhere)
class UBoxComponent* BoxCollision;
UPROPERTY(EditAnywhere)
class UStaticMeshComponent* Paddle;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
class UPaddleMovementComponent* OurMovementComponent;
virtual UPawnMovementComponent* GetMovementComponent() const override;
private:
FVector MovementDirection;
UPROPERTY(EditAnywhere, Category = "Movement")
float MovementSpeed = 20000.0f;
void MoveX(float Value);
};
Pawn CPP File
// Fill out your copyright notice in the Description page of Project Settings.
#include "Paddle_Player1.h"
#include "PaddleMovementComponent.h"
#include "Components/InputComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "UObject/ConstructorHelpers.h"
// Sets default values
APaddle_Player1::APaddle_Player1()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("RootComponent"));
RootComponent = BoxComponent;
BoxComponent->SetCollisionProfileName(TEXT("Paddle"));
BoxComponent->SetWorldScale3D(FVector(100.0f, 15.0f, 15.0f));
Paddle = CreateDefaultSubobject <UStaticMeshComponent>(TEXT("Pawn"));
Paddle->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> PaddleVisualAsset(TEXT("/Engine/Content/BasicShapes/Cube.uasset"));
if (PaddleVisualAsset.Succeeded())
{
Paddle->SetStaticMesh(PaddleVisualAsset.Object);
Paddle->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
Paddle->SetWorldScale3D(FVector(100.0f, 15.0f, 15.0f));
}
AutoPossessPlayer = EAutoReceiveInput::Player0;
OurMovementComponent = CreateDefaultSubobject<UPaddleMovementComponent>(TEXT("Custom Move Component"));
OurMovementComponent->UpdatedComponent = RootComponent;
}
// Called when the game starts or when spawned
void APaddle_Player1::BeginPlay()
{
Super::BeginPlay();
BoxComponent->SetSimulatePhysics(false);
BoxComponent->SetEnableGravity(false);
BoxComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
}
// Called every frame
void APaddle_Player1::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// if (!MovementDirection.IsZero()) {
// const FVector NewLocation = GetActorLocation() + (MovementDirection * DeltaTime * MovementSpeed);
// SetActorLocation(NewLocation);
// }
}
// Called to bind functionality to input
void APaddle_Player1::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAxis("P1_MoveX", this, &APaddle_Player1::MoveX);
}
UPawnMovementComponent* APaddle_Player1::GetMovementComponent() const
{
return OurMovementComponent;
}
void APaddle_Player1::MoveX(float Value)
{
if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
{
OurMovementComponent->AddInputVector(GetActorForwardVector() * Value);
}
//MovementDirection.X = FMath::Clamp(Value, -1.0f, 1.0f);
}
Movement Component Header File
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PawnMovementComponent.h"
#include "PaddleMovementComponent.generated.h"
/**
*
*/
UCLASS()
class PONGCLONE_API UPaddleMovementComponent : public UPawnMovementComponent
{
GENERATED_BODY()
public:
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
Movement Component CPP File
// Fill out your copyright notice in the Description page of Project Settings.
#include "PaddleMovementComponent.h"
#include "Paddle_Player1.h"
void UPaddleMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime))
{
return;
}
FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * DeltaTime * 20000.0f;
if (!DesiredMovementThisFrame.IsNearlyZero())
{
FHitResult Hit;
SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit);
if (Hit.IsValidBlockingHit())
{
SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time, Hit.Normal, Hit);
}
}
}
I have watched a bunch of tutorials and read documentation, but I cannot reproduce the same results in them whatsoever!
Attached below is the example image of the result, as well as the collision settings for the bottom wall.
Any assistance would be greatly appreciated.