How to make character face towards movement direction?

For the past couple of days I have been looking through all the unity answers and questions to find a solution to this problem. Now, I have found a sort of solution but it doesn’t work perfectly.
Not only does my character not turn go in the direction that it is facing but when I push my “turning buttons” (this causes the player to turn clockwise or counterclockwise) it causes the players to start rolling in a random direction.
Lastly, for some odd reason that I can’t even fathom why, my up/forward button goes completely to the right. The left goes the direction the forward/up button should go. I have tried starting my player at a different rotation or just moving the camera but it continues to go right.

Any help at this point would be deeply appreciated. Below is my script for my player controller.

{

public GameObject PlayerOne;

public float Movement;
public Rigidbody rb = null;
public float speed = 5.0f;
public float movementSpeed = 5.0f;
public float clockwise = 10.0f;
public float counterClockwise = -10.0f;

// Use this for initialization
void Start()
{
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
	
	if (Input.GetKey (KeyCode.W)) {
		transform.position += transform.forward * Time.deltaTime * movementSpeed;
	} else if (Input.GetKey (KeyCode.S)) {
		transform.position += Vector3.back * Time.deltaTime * movementSpeed;
	} else if (Input.GetKey (KeyCode.A)) {
		transform.position += Vector3.left * Time.deltaTime * movementSpeed;
	} else if (Input.GetKey (KeyCode.D)) {
		transform.position += Vector3.right * Time.deltaTime * movementSpeed;
	}
	if (Input.GetKey (KeyCode.E)) {
		transform.Rotate (0, Time.deltaTime * clockwise, 0);
	} else if (Input.GetKey (KeyCode.Q)) {
		transform.Rotate (0, Time.deltaTime * counterClockwise, 0);
	}
}

}

Hi,

Instead of directly modifying the position of the transform, try using Transform.Translate with it set to self space.

In this case you can use Vector3.forward, Vector3.right etc. instead of transform.forward because the translation will occur in local space and not world space.

If you want the character to always be rotated in the direction of travel you need to use the following

Transform.rotation = Quaternion.LookAt(transform.forward, Vector3.up);

If you want it to smoothly rotate towards the direction of travel its a bit more work. You will have to cache the desired rotation and continuously lerp towards instead of setting it directly.

Hi
Above script mentioned by you is working fine for me.I think you have to set the directions of the character. The character should face the z axis and x axis on the right and y axis to their upward direction.

Hey there!

There are a few things I would change in your code as well as some suggestions for your player object.

First, there is a huge difference between transform.back and Vector3.back/left/right (which you use for everything except W): they are not aligned to your transforms rotation!

You also don’t need a Rigidbody unless you want ragdoll effects at one point. The player character should be kinematic anyway.

Finally, a recommendation: put the visual representation of the player into a child game object of your player game object, so that you can rotate it separately from the player in case it is in a different coordinate system as Unity (usually that’s the case).

Thank you all for all your answers. I tried each one of them but none of them seemed to work (I tried the Quaternion and got errors, I moved my whole thing to have everything face the z axis but it continued to go right).
Until I decided to do something a little weird/dumb. I changed my W to Vector3.forward instead of transform.forward and it works perfectly fine now. When I hit my turn keys though it still rotates funny but that is something I can probably mess around with and fix another time.
Thank you all though for your suggestions, I really appreciate it!