Hi, I’m working on a third person game, but I’m struggling trying to achieve Pawn movement the way I want: move the Pawn BASED on camera direction.
So if the camera is facing NORTH and I go to the left, Pawn should move to the WEST or if the camera is facing WEST and I go to the left, Pawn should move to the SOUTH etc… I already did it with a Character component using “FRotationMatrix” and “AddMovementInput” functions, but it doesn’t work with Pawn.
Here is my code for reference:
#include "Ball.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
// Sets default values
ABall::ABall()
{
// 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;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(GetRootComponent());
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(Mesh);
CameraBoom->TargetArmLength = 400.f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
}
// Called when the game starts or when spawned
void ABall::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABall::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ABall::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ABall::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABall::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
}
void ABall::MoveForward(float value)
{
if (Controller != nullptr && value != 0.f)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0.f, Rotation.Yaw, 0.f);
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, value);
}
}
void ABall::MoveRight(float value)
{
}