Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.15
more ▼

asked Apr 03 '17 at 12:52 AM in C++ Programming

avatar image

1 1 2 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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.


 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:


more ▼

answered Apr 05 '17 at 12:47 AM

avatar image

111 1 3 7

avatar image theoncomingfish Apr 05 '17 at 06:09 PM

Thanks! This worked great. I appreciate the assistance!

avatar image theoncomingfish Apr 05 '17 at 08:14 PM

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.

avatar image LaOwLLol Apr 05 '17 at 08:49 PM

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.

avatar image theoncomingfish Apr 05 '17 at 08:58 PM

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.

avatar image theoncomingfish Apr 05 '17 at 09:09 PM

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

avatar image LaOwLLol Apr 05 '17 at 10:01 PM

No problem, glad I could help.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Apr 03 '17 at 05:41 AM

avatar image

219 4 3 4

avatar image theoncomingfish Apr 03 '17 at 10:06 PM

alt text

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.

movement.png (15.8 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question