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);

If you are moving towards specific vector3 point you can use Transform.MoveTowards().

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.

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.