Turning Characters to face moving direction - Quaternion.Lerp help

Hi, I have a script for a character controller. Currently it works but when I let go of a key it sets the rotation back to 0, which considering my script it SHOULD do. I don’t know what to put there instead.

Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), >>>HERE<<<, Input.GetAxis("Vertical"));

Because having that means if I put 0f in the >>>HERE<<< section then it will set rotateY back to 0 each update. What do I put there that will retain it’s rotation on Y? I have tried so many things.

Here is the full code. Please take a look, thanks very much.

using UnityEngine;
using System.Collections;

public class TP_Controller : MonoBehaviour
{
	public float runSpeed = 5f;
	public float turnSpeed = 20f;
	public float gravity = 21f;
	public float jumpSpeed = 12f;
	
	private Vector3 moveVector = Vector3.zero;
	
	public CharacterController CharacterController;
	
	void Awake()
	{
		CharacterController = gameObject.GetComponent("CharacterController") as CharacterController;
	}
	
	
	void Update()
	{
		ProcessMovement();
	}
	
	
	void ProcessMovement()
	{
		moveVector = new Vector3(Input.GetAxis("Horizontal"), moveVector.y, Input.GetAxis("Vertical"));
		
		if(CharacterController.isGrounded  Input.GetButton("Jump"))
		{
			moveVector.y = jumpSpeed;
		}
		
		ProcessGravity();
		
		Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), Quaternion.identity.y, Input.GetAxis("Vertical"));
		Quaternion lookRotation = Quaternion.LookRotation(currentRotation);
		
		transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
		
		
		
		CharacterController.Move(moveVector * Time.deltaTime);
	}
	
	void ProcessGravity()
	{
		if(!CharacterController.isGrounded)
		{
			moveVector.y -= gravity * Time.deltaTime;
		}
	}
	
}

transform.eulerAngles.y

I think… the y component of the vector3 representation of the transform’s current orientation :slight_smile:

That’s what I thought, but using it just causes weird rotations and it still just flips back to 0 on y.

transform.rotation.y instead of Quaternion.identity.y? I don’t know…

Same thing.

If I think about it some more it’s not SETTING the rotation.y to 0f, it’s not affecting the rotation.y at all because it’s 0f. Which means it SHOULDNT rotate to face the front each time… but it is.

So why would this cause the player to rotate forwards each time?

Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
Quaternion lookRotation = Quaternion.LookRotation(currentRotation, Vector3.up);

transform.rotation = Quaternion.Lerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);

Doing this helped a bit, but still when I rotate it snaps a little into another direction…

Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

if(currentRotation != Vector3.zero)
{
	Quaternion lookRotation = Quaternion.LookRotation(currentRotation, Vector3.up);

	transform.rotation = Quaternion.Lerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
}

One thing I noticed is in the LookRotation function your suposed to use direction vectors not rotations. Maybe try Quaternion.Euler(currentRotation) instead. Or maybe transform.Rotate(currentRotation,Space.Self).

Thanks :slight_smile: Not having luck with either of those, though. Quaternion.Euler(currentRotation) causes it to not even rotate.

transform.Rotate not sure how to use that properly.

Vector3 finalRotation = transform.Rotate(currentRotation, Space.Self);

= Cannot implicitly convert type void' to UnityEngine.Vector3’

transform.Rotate rotates the object the script is on unless you use something like object2.transform.Rotate.

I can’t use that with a quaternion, and since I need lerp to smooth the turning it wont work.