Make the player face his movement direction

I’ve just made a few changes to my code:

void Update () {
	
		ControllPlayer();
	}


	void ControllPlayer()
	{
		float moveHorizontal = Input.GetAxisRaw ("Horizontal");
		float moveVertical = Input.GetAxisRaw ("Vertical");

		Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
		transform.rotation = Quaternion.LookRotation(movement);


		transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);

		
		if(Input.GetButtonDown ("Fire1"))
		{
			animation.Play ("attack-01");
		}
	}

Now the only thing that’s wrong with it is, that when I stop pressing the movement keys on the gamepad, the Player turns automatically in the forward direction.
I would like him to stay as he was when moving. Is there a quick fix to this?

the problem was that I was moving in Space.Self instead of Space.World.
fixed and done.

float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");
 
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.rotation = Quaternion.LookRotation(movement);
 
 
transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);

Sorry for the necropost, but I am sure a lot of people are probably stumbling across this question. I myself a few hours ago found this question while searching for “How to make the player face movement direction” Now I noticed this question is already solved, and the answer works great. After doing a bit of research on Slerp, I made it so the script (that PaxForce made) will smoothly turn instead of doing a sharp turn when rotating.

The way it was done was by replacing transform.rotation = Quaternion.LookRotation(movement); with transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);

If anyone wanted to know how it works, Quaternion.Slerp takes 3 arguments: 2 Quaternions and 1 Float. It interpolates the rotation between the 2 Quaternions with a speed of the Float value (0.0 is no movement while 1.0 is Instant Movement.) The code I did interpolates the rotation between the Current Rotation (transform.rotation) and the Movement Rotation (Quaternion.LookRotation) with a speed of 0.15F.

Hope this helped someone out there.

@hellopeople0004
Your answer was right on time. Works perfectly.

Here’s my end result. Nice and smooth.

if(movement != Vector3.zero)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized), 0.2f);

A way to handle this is to set the transforms forward to your normalized move direction, then when he has to walk, add a Vector3.forward force times the speed. For example:

void Update()
{
    Vector3 dir = <your movedirection (_movement)>.normalized;
    transform.forward = dir;
    transform.Translate(Vector3.forward * <speed (_movementspeed)> * Time.deltaTime)>);
}

GL! :slight_smile:

I got this to work but my character after he stops moving he faces towards +z does Anyone know how to make him keep facing the way that he was just moving @PaxForce

@PaxForce just put this line transform.rotation =Quaternion.LookRotation(movement); into an if statement

if (movement != Vector3.zero) {
		transform.rotation =Quaternion.LookRotation(movement);
	}

this works for me.

Is there a way someone has found to make this work with a third person camera, the camera is all werid since mine is not sitting directly on the character`public class PlayerMovement : MonoBehaviour
{
private Rigidbody rb;
public float moveSpeed;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
    float horizMove = Input.GetAxisRaw("Horizontal");
    float vertMove = Input.GetAxisRaw("Vertical");

    Vector3 movement = new Vector3(horizMove, 0.0f, vertMove);
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);

    transform.Translate(movement * moveSpeed * Time.deltaTime, Space.World);
}

}`

set your local scale to either 1 or -1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player1 : MonoBehaviour {

public float speed;

void Start () {
}

void Update(){
	Mouvement ();
}

void Mouvement (){
	
	float moveHorizontal = Input.GetAxis ("Horizontal");
	float moveVertical = Input.GetAxis ("Vertical");

	Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

	transform.Translate (movement * speed * Time.deltaTime, Space.World);

	if (movement != Vector3.zero)
		transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.normalized), 0.2f);
}

}

Been searching for a quick and simple move and face script myself, and ended up making this one.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCar : MonoBehaviour
{
    public GameObject objectToMove;
    public float speed = 5.0f;

    void Update()
    {
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
            transform.rotation =Quaternion.LookRotation(Vector3.right);
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.position += Vector3.left* speed * Time.deltaTime;
            transform.rotation =Quaternion.LookRotation(Vector3.left);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.position += Vector3.forward * speed * Time.deltaTime;
            transform.rotation =Quaternion.LookRotation(Vector3.forward);
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.position += Vector3.back* speed * Time.deltaTime;
            transform.rotation =Quaternion.LookRotation(Vector3.back);
        }
    }
}