I’m trying to implement some functionality to my game in UE5 that works like snaptap. Here’s the code i’m using as a base:
void APlayerCharacter::Move(const FInputActionValue& Value) {
FVector2D InputVector = Value.Get();
if (IsValid(Controller))
{
//Get Forward Direction
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector ForwardVector = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector SideVector = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// Add Movement Input
AddMovementInput(ForwardVector, InputVector.Y);
AddMovementInput(SideVector, InputVector.X);
}
What I noticed is that when i’m moving right and press the left direction key the character changes directions without having to let go of the right direction key, but the opposite doesnt happen. I want to be able to make the last input override the previous one without having to let go of the first input if they are in opposite directions. Functioning like snaptap. Does anyone have an idea of how should i go about it to get the desired behaviour?
Thanks in advance.
I tried this other code:
void APlayerCharacter::MoveRightForward(const FInputActionValue& Value) {
FVector2D InputVector = Value.Get();
if (IsValid(Controller))
{
// Get Forward and Side Directions based on the Yaw
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector ForwardVector = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector SideVector = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// Apply movement only for positive input values (right and forward)
if (InputVector.X > 0.0f) // Right movement
{
AddMovementInput(SideVector, InputVector.X);
}
if (InputVector.Y > 0.0f) // Forward movement
{
AddMovementInput(ForwardVector, InputVector.Y);
}
}
This makes the character stand still in place if inputs in opposite directions are pressed at the same time.