How do I move in the direction the pawn faces instead of the camera

I’m starting from the c++ third person start and am trying to change the way movement works a bit. What I want to happen is the character will rotate either left or right using the a and d keys and then pressing w will make the character move in the direction the mesh is facing. I want the player to have full control over the camera at the same time, so that no matter where they face it the character will still move where the mesh is faced. It seems that everything is relative to the camera, however, causing the character to spin if I attempt to move it in the direction of the mesh. Any idea how I can fix this? I don’t want to separate the pawn and camera, just set the controller to rotation to be the same as the mesh.

Mind giving a screenshot of the code that doesnt work as intended? Might be easier to help explain how it should be done. For now all i can say is to use the pawns forward vector.

132713-movement.png

Yeah, here it is. I tried using the mesh’s forward vector for movement instead of the camera, and the character just spun in circles. I understand why that is and why this doesn’t work but I’m not sure how I can make the controller rotation match the pawn without the camera matching.

It sounds like your camera is inheriting controller rotation and your character is not. If you want the camera to rotate with controller input and the character to move without the camera this is fine. I think your code is very close, but you want to move in the direction of the character’s forward vector not necessarily the character mesh’s forward vector.

Try:

FVector direction = GetActorForwardVector();
AddMovementInput(direction, Value);

If you are not already using a SpringArm, you may want to look into attaching one to your character and then attaching your camera to the SpringArm. A Spring Arm will help you decouple the code for your character’s movement from the camera movement, and “automatically control how the camera handles situations where the it becomes obstructed by level geometry or other objects in the level.” Here is some more information about that:

Thanks! This worked great. I appreciate the assistance!

Sorry to ask again, but is there any way to do this in reverse? Having the s key set to make the value of the function -1 just seems to make the character move towards the -x axis of the world as a whole as does setting Direction to the inverse of the actors forward vector.

In your unreal project input settings make sure your axis mapping for ‘S’ key is -1.0 scale.

Let’s say the ‘S’ key is under an axis named “Reverse”. All you need to do in your SetupPlayerInputComponent method, is bind Reverse to MoveForward.

By setting the scale for the Reverse axis to -1.0, and binding the axis to MoveForword we tell the engine to pass a negative Value to your MoveForward method.

You could also make a similar function MoveBackword, keep the reverse axis to scale at +1.0, bind reverse axis to MoveBackword, but in your MoveBackward method pass -Value to AddMovementInput.

I’ve actually tried both of those things, but I think the problem is that because the character tries to turn to face the direction it is moving, that causes the forward vector to change and makes it panic.

Nevermind, I just had to shut off orient rotation to movement and it works flawlessly. Thanks again for all the help!

No problem, glad I could help.