Download

Problem getting forward vector

Hi all!

I’m following this tutorial https://docs.unrealengine.com/en-US/…2/3/index.html and I have a problem getting the forward vector (the right vector works great). By getting the scaled X axis of the controller’s rotation I’m supposed to avoid dependency of the other axis value, meaning that if I look up or down, the character will get the same forward and move the same distance over time. That’s the same thing I do with the right vector, and it doesn’t matter where I look to, the movement to the right/left works perfect. But with the forward doesn’t work as intended. The higher I look, the faster I move, and the more down, the slower. I have binded the functions properly, of course. The code this (its almost the same as in the tutorial):

voidAFPSController::MoveForward(float AxisValue)
{
//Get the forward vector
FVector direction = FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::X);
AddMovementInput(direction, AxisValue);
}

voidAFPSController::MoveRight(float AxisValue)
{
//Get the right vector
FVector direction = FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::Y);
AddMovementInput(direction, AxisValue);
}

Any clue what might happen here?

Thanks!

The reason you move slowest when you are not looking straight ahead is because those vectors are wasting some of their length going upward/downwards which results in less length to go forward, hence slower speeds. You need to zero your Z axis on your direction vector:



voidAFPSController::MoveForward(float AxisValue) {
    //Get the forward vector
    FVector direction = FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::X);

    // We get the flattened forward look direction and normalize it
    direction = FVector(direction.X, direction.Y, 0.f).GetSafeNormal();    
    AddMovementInput(direction, AxisValue);
}


This gives the direction your player is looking on a lateral plane.

Works like a charm! Thanks! Now I have a doubt, why is the tutorial wrong then? They say specifically that that’s the way (the bad one) to do it to avoid this exactly problem… And why it works proeprly with the MoveRight without making the Z = 0?

Which tutorial? Do you have a link? Maybe it can be updated.

I have no idea on why the wiki is showing that, I also found this with a quick Google search immediately, and they also followed that tutorial with the same problem so it’s not a new one.

For why MoveRight works without zero-ing the Z axis, you never rotate your right vector up or down (I’m going to assume you are making a standard character that moves along a floor). You only rotate your right vector on the yaw axis when moving your camera left and right, while your forward vector rotates up and down on your pitch axis due to rotating the camera up and down.