I have created a pawn that moves along the x and y axis, and rotates left and right. However, when I rotate, the pawn still moves along the original x and y axis, so it doesn’t look right when it moves after rotating. How would I use the pawns forward and right vectors to correct the movement so that the pawn still moves correctly?
Hi ! Looks like a duplicated question, check the answer here :
Unfortunately, when I tried to implement this, it didn’t work. I don’t know why, but apparently this approach doesn’t work for me. Thank you though.
Then maybe your rotation code is doing something wrong, do you rotate controler ? Pawn ? Camera ?
I’m rotating a pawn. I have a variable which gets the pawns rotation and then adds/subtracts depending on the buttons pressed. However, the vectors do not rotate and I cannot figure out how to get them to rotate.
Could you share:
-your moving code + name of method + class it is in
-your turning code + name of method+ class it is in
void ADuck::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Turning left and right
FRotator TurningValue = GetActorRotation();
if (bTurningLeft)
{
TurningValue.Yaw -= 1;
}
if (bTurningRight)
{
TurningValue.Yaw += 1;
}
SetActorRotation(TurningValue);
// Move in all directions, but restrict movement to set distance so duck stays on water
if (!Velocity.IsZero())
{
FVector NewDuckLocation = GetActorLocation() + (Velocity * DeltaTime);
NewDuckLocation.X = FMath::Clamp(int(NewDuckLocation.X), 150, 665);
NewDuckLocation.Y = FMath::Clamp(int(NewDuckLocation.Y), 120, 675);
SetActorLocation(NewDuckLocation);
}
}
void ADuck::MoveForwardBackward(float AxisValue)
{
// Move along the x-axis using controls set in the project settings
Velocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 500.0f;
}
void ADuck::MoveLeftRight(float AxisValue)
{
// Move along the y-axis using controls set in the project settings
Velocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 500.0f;
}
void ADuck::LeftTurn()
{
bTurningLeft = true;
}
void ADuck::LeftStop()
{
bTurningLeft = false;
}
void ADuck::RightTurn()
{
bTurningRight = true;
}
void ADuck::RightStop()
{
bTurningRight = false;
}
// Input functions for movement, turning
void MoveForwardBackward(float AxisValue);
void MoveLeftRight(float AxisValue);
void LeftTurn();
void LeftStop();
void RightTurn();
void RightStop();
// Input variables for movement, turning
FVector Velocity;
bool bTurningLeft;
bool bTurningRight;
This is all the code to do with turning and moving at the moment.
so using
FVector NewDuckLocation = GetActorLocation() + (TurningValue.RotateVector(Velocity) * DeltaTime);
doesn’t work ?
remove your binary folder and rebuild to be sure to have up to date binaries
This works for moving along the y axis, or left and right, but when I attempt to move forward and backward along the x axis, I find that I can only move forward no matter which button I press, and if I try to go forward I end up going down and through the floor, and if I go backwards I end up going up and floating above the floor. I will try using clamp on the z axis of the duck, but I’m still not sure what to do about the only moving forwards thing.
And by clamping the z axis, I now cannot move forward and backward at all
I fixed it by changing the direction of movement from the x axis to the z axis, thank you for pointing me in the right direction
Then maybe try :
FVector NewDuckLocation = GetActorLocation() + (FRotator(0, TurningValue.Yaw, 0).RotateVector(Velocity) * DeltaTime);