How can I get my pawn moving towards its forward direction

Hey,

I’m trying to make a pawn that will move towards the forward direction whenever the player is pressing the input to do so. Currently I can make it move up and down the X axis, however whenever the player rotates the object using the mouse it will continue to only move along the X-Axis, rather than moving forwards towards the direction it is facing. I’ve posted the code I have below
#include “PlayerCharacter.h”
#include “Cplusplusfirsttry.h”
#include “Camera/CameraComponent.h”
#include “Components/InputComponent.h”
#include “Components/StaticMeshComponent.h”
#include “GameFramework/PlayerController.h”
#include “Engine/LocalPlayer.h”
#include “GameFramework/DefaultPawn.h”
#include “GameFramework/SpringArmComponent.h”

float InputValue;
// Sets default values
APlayerCharacter::APlayerCharacter()
{
 	// 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;
	AutoPossessPlayer = EAutoReceiveInput::Player0;
	
	//create a camera and a mesh, then set the root component to the mesh
	PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
	PlayerMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	RootComponent = PlayerMesh;
	//create springarm and attach it and mesht to root component. attach camera to springarm
	CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm")); //creating a springarm for the camera
	CameraSpringArm->SetupAttachment(RootComponent);
	CameraSpringArm->TargetArmLength = 0.0f; //how far from the spring arm the camera is. As it is first person this will be 0
	CameraSpringArm->SetWorldRotation(FRotator(0.0f, 0.0f, 0.0f)); //initialising roation. should start directly straight
	
	PlayerCamera->SetupAttachment(CameraSpringArm, USpringArmComponent::SocketName); //attaches camera to end of spring arm
	PlayerSpeed = 250.0f;
	
}


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

// Called every frame
void APlayerCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//handles movement based on the MoveX and MoveY axes
	if (!CurrentVelocity.IsZero())
	{
		FVector Direction = GetActorForwardVector();
		FVector NewLocation = (GetActorLocation() + (CurrentVelocity * DeltaTime));
		SetActorLocation(NewLocation);
	}
	FRotator NewX = GetActorRotation(); //allows x axis camera to rotate with character
	NewX.Yaw += MouseInput.X;
	SetActorRotation(NewX);

	FRotator NewY = CameraSpringArm->GetComponentRotation(); //means character will not rotate on Y axis when looking up
	NewY.Pitch = FMath::Clamp(NewY.Pitch += MouseInput.Y, -60.f, 60.f);
	CameraSpringArm->SetWorldRotation(NewY);
}

// Called to bind functionality to input
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	//respond to the MoveX and MoveY commands
	PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter::Move_Forward);
	PlayerInputComponent->BindAxis("MoveSide", this, &APlayerCharacter::Move_Side);
	//respond to mouse movement
	PlayerInputComponent->BindAxis("MouseX", this, &APlayerCharacter::MouseX);
	PlayerInputComponent->BindAxis("MouseY", this, &APlayerCharacter::MouseY);
	
}
void APlayerCharacter::Move_Forward(float Input)
{
	
	
	CurrentVelocity.X = FMath::Clamp(Input, -1.0f, 1.0f) * PlayerSpeed;
	
}
void APlayerCharacter::Move_Side(float Input)
{

	
}
void APlayerCharacter::MouseX(float AxisValue)
{
	MouseInput.X = AxisValue;
}
void APlayerCharacter::MouseY(float AxisValue)
{
	MouseInput.Y = AxisValue;
}

Your problem is the combination of this line:

CurrentVelocity.X = FMath::Clamp(Input, -1.0f, 1.0f) * PlayerSpeed;

…with these lines:

FVector Direction = GetActorForwardVector();
FVector NewLocation = (GetActorLocation() + (CurrentVelocity * DeltaTime));
SetActorLocation(NewLocation);

You seem to be almost there, but your code doesn’t appear to make use of your character’s forward vector as intended. (Even though you store the forward vector in a variable, the variable isn’t used!)

You should be able to get your code working to some extent by just incorporating your Direction vector. Maybe something like:

FVector Direction = GetActorForwardVector();
FVector NewLocation = (GetActorLocation() + (CurrentVelocity.X * Direction * DeltaTime));
SetActorLocation(NewLocation);

But, even with that change, I wonder whether you shouldn’t consider a slightly different (and simpler) approach.

If you haven’t already, I suggest taking a look at the C++ code that is generated with the First Person template, specifically the Player Controller and Pawn classes. Even if you’re not making a first-person game, the code examples from that template show how to neatly use inputs to call very small functions that handle movement in all directions and rotation of the pawn using the mouse. I think you might find it useful to adapt some of the simplicity of that template code for your character class. (Just a suggestion! Totally up to you.)

Thanks a bunch dude, that worked great. I feel kind of silly for not getting it now lol.