- Home /
Character MoveLook rotation locked?
Hey there! So I have tried to make my Character Rotate when I am walking around with Vertical and Horizontal Axis. Though the closest I got to make it work was this script. Though my only problem with the script is that when I stop moving it resets it self by looking the same direction as when I spawn the player. Anyone who knows why?
using UnityEngine;
using System.Collections;
public class MoveLook : MonoBehaviour {
public float lookSpeed = 10;
private Vector3 curLoc;
private Vector3 prevLoc;
void Update ()
{
InputListen();
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
}
private void InputListen()
{
prevLoc = curLoc;
curLoc = transform.position;
if (Input.GetKey (KeyCode.A))
curLoc.x -= 1 * Time.fixedDeltaTime;
if (Input.GetKey (KeyCode.D))
curLoc.x += 1 * Time.fixedDeltaTime;
if (Input.GetKey (KeyCode.W))
curLoc.z += 1 * Time.fixedDeltaTime;
if (Input.GetKey (KeyCode.S))
curLoc.z -= 1 * Time.fixedDeltaTime;
transform.position = curLoc;
}
}
Answer by Baste · Oct 02, 2014 at 02:16 PM
What's happening is that Quaternion.LookRotation(transform.position - prevLoc) makes no sense when you're standing still - as transform.position and prevLoc is the same, you're getting the lookRotation for 0. That should log an error, by the way.
A simple fix would simply be to check if the player is moving before you rotate it:
if(transform.position != prevloc) {
transform.rotation = ...
}
That will probably cause some problems - if you're facing right and quickly press 'a', your character won't turn all the way around. If that's a problem or not is completely dependent on how your game works.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164234im_/https://answers.unity.com/themes/thub/images/avi.jpg)