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?

1 Like

This line here: transform.rotation = Quaternion.LookRotation(_movement.normalized); Is a pretty standard way of getting an object to face the move direction (don't need '.normalized'). How is it failing? If the rotation of your character is (0,0,0), is the front side facing positive 'z'?

Also is your model oriented correctly?

how about if(moveHorizontal != 0 && moveVertical != 0) { transform.rotation = Quaternion.LookRotation(movement); }

thanks for the idea - I can't believe how easy this was to fix :) ...althought...you got one thing wrong. the correct way is this: if(moveHorizontal != 0 || moveVertical != 0) { transform.rotation = Quaternion.LookRotation(movement); }

that's a matter of taste :D

10 Answers

10

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

How did you end up exactly fixing this? I'm still stuck with this problem and the code you posted here is the same as the one in your Question. I don't get how to set my player to move in space,world instead of space.self

whenever i do this my character rotates 90 degrees along the Z axis does anyone have a fix??

THANKS A LOT!!! it works like a charm :) however you could use the slerp to smooth it out.

Thanks dude! i had no idea you can use .LookRotation();, but now i do. This really helped me progress as a developer, i'm new.

I have a problem, it tells me that "movementspeed " is not in the actual context. Im new btw

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.

I was looking all over the place for this solution. Works like a charm. Not sure why so many turning examples and tutorials didn't mention this.

Thank You so much This works perfectly

Thanks my dude! It's much better now. looks pretty smooth, especially when you turn 180 degrees.

Wow thanks , very usefull !

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

No Problem, glad it helped :)

Thanks! I couldn't stop my object from rotating back to the default position.

@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.

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:

dude...did you try this piece code out? because it's not working for me.

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

Found something?

Look at @ThreeYams 's answer. The if (moveDirection != Vector3.zero) before the rotation should do it

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

}

how do you incorporate this with third person camera so the character still faces camera direction when running forward

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