APawn class, add axis mapping to enable control of APawn movement
MyPawn.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/StaticMeshComponent.h"
#include "Camera/CameraComponent.h"
#include "MyPawn.generated.h"
UCLASS()
class FIRSTDEMO_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
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;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MyStaticMesh;
UPROPERTY(VisibleAnywhere)
UCameraComponent* MyCamera;
UPROPERTY(EditAnywhere)
float MaxSpeed;//add pawn movement speed parameter.
private:
// add axis mapping function, MoveForward, MoveRight, Velocity
//Set to private variables, handled in the Pawn class itself, no need to expose them to outsiders.
void MoveForward(float Value);
void MoveRight(float Value);
FVector Velocity;
};
MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "Components/InputComponent.h"
// Sets default values
AMyPawn::AMyPawn()
{
// 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;
RootComponent=CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
MyStaticMesh->SetupAttachment(GetRootComponent());
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(GetRootComponent());
MyCamera->SetRelativeLocation(FVector(-300.f, 0.f, 300.f));
MyCamera->SetRelativeRotation(FRotator(-45.f, 0.f, 0.f));
AutoPossessPlayer = EAutoReceiveInput::Player0;
MaxSpeed = 100.f;
Velocity = FVector::ZeroVector;
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//Add offset function to tick.
AddActorLocalOffset(Velocity * DeltaTime, true);
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Bind player input to axis mapping, axis mapping and function name should correspond to each other
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AMyPawn::MoveForward);
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AMyPawn::MoveRight);
}
void AMyPawn::MoveForward(float Value)
{
//Forward speed, use Clamp to limit value to -1,1, place too large a value in the Ue4 editor, resulting in a bug.
Velocity.X = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}
void AMyPawn::MoveRight(float Value)
{
// Speed to the right, use Clamp to limit value to -1,1, place too large a value in the Ue4 editor, resulting in a bug.
Velocity.Y = FMath::Clamp(Value, -1.f, 1.f) * MaxSpeed;
}