I’m trying to move a C++ Pawn class on axis Z.
I have defined this on Unreal Editor → Project Properties…
And this is the C++ Pawn class body:
#include "Paddle.h"
#include "Components/BoxComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/InputComponent.h"
// Sets default values
APaddle::APaddle()
{
// 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;
// Set this pawn to be controlled by the lowest-numbered player
AutoPossessPlayer = EAutoReceiveInput::Player0;
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
// Disable collision.
MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
// Set the first object in the hierachy.
RootComponent = MeshComp;
CollisionComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionComp"));
// No physics, queries only.
CollisionComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
CollisionComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
// It will generate the 'Hit' event.
CollisionComponent->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Block);
CollisionComponent->SetupAttachment(MeshComp);
}
// Called when the game starts or when spawned
void APaddle::BeginPlay()
{
Super::BeginPlay();
}
void APaddle::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Respond every frame to the values of our movement.
InputComponent->BindAxis(TEXT("MovePaddle"), this, &APaddle::Move_ZAxis);
}
void APaddle::Move_ZAxis(float AxisValue)
{
CurrentVelocity.Z = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}
// Called every frame
void APaddle::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Handle movement based on our "MoveZ" axis.
if (!CurrentVelocity.IsZero())
{
const FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation);
}
}
And this is the header file:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Paddle.generated.h"
class UBoxComponent;
UCLASS()
class PONG_API APaddle : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APaddle();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// Input functions
void Move_ZAxis(float AxisValue);
UPROPERTY(VisibleAnywhere, Category = "Components")
UBoxComponent* CollisionComponent;
UPROPERTY(VisibleAnywhere, Category = "Components")
UStaticMeshComponent* MeshComp;
// Input variables
FVector CurrentVelocity;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
To try it, I press Play button on editor but when I press R or F key nothing happens.
I’ve been searching for a tutorial about how to do it, or sample code, or book, but it seems that I’m not searching correctly because I haven’t found anything useful.
Why it doesn’t work?
By the way, I have blueprinted the C++ to add it to the editor.