- Home /
How to make my player look in the direction of its movement?
I've looked around the forums for an answer for this question, but haven't found one that works, yet.
My player moves in relation to the direction of the camera, i.e. forward = camera.forward, right = camera.right etc.
I've tried using things like setting the transform.rotation to Quaternion.LookRotation, but that messes up the movement really bad.
I've been tearing my hair over this, maybe one of you guys can help me.
Here's my code for the movement (all in Update):
maxSpeed = 0.25f
float forwardAxis = Input.GetAxis ("Vertical");
float sideAxis = Input.GetAxis ("Horizontal");
Vector3 forward = (camera.transform.forward * forwardAxis) * maxSpeed;
Vector3 sideways = (camera.transform.right * sideAxis) * maxSpeed;
forward.y = 0;
sideways.y = 0;
movement = new Vector3 (sideways.x + forward.x, sideways.y+forward.y, sideways.z + forward.z);
transform.Translate (movement);
Answer by Cheaterman24 · Apr 15, 2015 at 07:13 PM
I've actually solved the problem I had myself.
In the camera script I am using, I wanted something a little better than just childing the camera to the player, but the way I fixed the problem was making the model a child of another empty gameobject and only applying the movement to the parent gameobject. I then apply the rotation to the child model(using Quaternion.LookRotation() ), and it makes the player look in the direction of movement without affecting the movement.
Answer by YoungDeveloper · Apr 14, 2015 at 10:31 PM
If you are moving towards specific vector3 point you can use Transform.MoveTowards().
Answer by toddisarockstar · Apr 15, 2015 at 01:26 AM
is this JS or C#????
actually, its easier than you think. in this case, you make the camera a parent of the character you want to move. Then the camera simply follows position and rotation of the object automatically without code.
simply drag and drop the camera into the player character and it will follow him. horray!
after that, the answer above me from (young developer) is correct. your verticle axis will pry rotate though.
post your entire code and we can fix.
Your answer
Follow this Question
Related Questions
Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers
Camera Rotating when the player reach a specific position 0 Answers
How to smoothly rotate camera around an object with touch 1 Answer
Choppy movement of moving quad with rotating camera 1 Answer
What is difference between local and global coord Vector3. 1 Answer