Currently, I can move the player in all directions using the WASD keys and the player always faces the direction he is running. But how do you keep that direction when you let go of the keys? So if I press “S” and let go, I should be able to see the front of my character because the rotation will be kept intact.
public float moveSpeed;
public float rotationSpeed;
Vector3 previousLocation;
Vector3 moveDirection;
void Update ()
{
moveDirection = Vector3.zero;
previousLocation = transform.position;
if(Input.GetKey(KeyCode.W))
{
moveDirection.z = 1;
}
if(Input.GetKey(KeyCode.S))
{
moveDirection.z = -1;
}
if(Input.GetKey(KeyCode.A))
{
moveDirection.x = -1;
}
if(Input.GetKey(KeyCode.D))
{
moveDirection.x = 1;
}
transform.position = Vector3.Lerp(transform.position, transform.position + moveDirection.normalized, Time.fixedDeltaTime * moveSpeed);
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
}