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;
}