Hi, I want to make the first person character move forward 100 cm when a key is pressed.
Also, I want it to rotate 90 degrees clockwise when another key is pressed.
I am using the first person template in C++.
Any help in C++ or Blueprint will be great.
Hello reward,
I will assume you have already setup your keys in Project Settings → Input.
Your character class should look something like this:
UCLASS()
class AMyCharacter : public ACharacter
{
GENERATED_BODY()
private:
void OnRotateInput();
void OnMoveInput();
public:
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
};
The implementation of the methods should look as follows:
void AMyCharacter::OnRotateInput()
{
FRotator CurrentRotation = GetActorRotation();
CurrentRotation.Yaw += 90.f;
SetActorRotation(CurrentRotation);
}
void AMyCharacter::OnMoveInput()
{
SetActorLocation(GetActorLocation() + GetForwardVector() *100)
}
void AMyCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
check(InputComponent);
InputComponent->BindAction("Move", IE_Pressed, this, &AMyCharacter::OnMoveInput);
InputComponent->BindAction("Rotate", IE_Pressed, this, &AMyCharacter::OnRotateInput);
}
Cheers,
Univise
This assumes your forward vector is X which is not always the case. I prefer something like:
SetActorLocation(GetActorLocation() + (GetActorForwardVector() * 100));
What was I thinking? Of course it has to be the forward vector. Thank you for pointing that out.