Quaternion.Slerp problem...

Hello!

I have this script that is supposed to turn the player smoothly on the direction that he walks. But the problem is that it turns immediatly, like the electrons of CERN!!! What’s wrong?


#pragma strict
var speed : float = 0.7;
var playergraphic : GameObject;
var RotSpeed : float;
private var newRot = Quaternion.Euler(0,90,0);
function Start () {

}

function Update () {
	var CurrentSpeed = Input.GetAxis("Horizontal") * speed;
	
	rigidbody.velocity = Vector3(CurrentSpeed,0,0);
	playergraphic.transform.rotation = Quaternion.Slerp(transform.rotation, newRot, Time.time * RotSpeed);
	
	if(rigidbody.velocity.x > 0)
	{
		newRot = Quaternion.Euler(0,90,0);
		playergraphic.animation.CrossFade("Walk");
	}
	else if(rigidbody.velocity.x < 0)
	{
		newRot = Quaternion.Euler(0,270,0);
		playergraphic.animation.CrossFade("Walk");
	}
	else {
		playergraphic.animation.CrossFade("Idle");
	}
}

The player has the main capsule collider as a parent object, and the graphicks are on a child object, that I want t turn. This is attached to the variable “playergraphic”

Time.time is not what you want there.

Time.time is 1 after 1s meaning that after 1 sec your rotation will occur instantly.

You may want to use Time.deltaTime * rotSpeed instead. With rotSpeed being there to regulate the speed of rotation.

Also you may want to try something like:

newRot= transform.rotation * Quaternion.Euler(0, 90, 0);

That would avoid all the if and simply add 90 degrees to the existing rotation.

EDIT:
It probably does not do anything because it should be :

newRot= playergraphic.transform.rotation * Quaternion.Euler(0, 90, 0);
playergraphic.transform.rotation = Quaternion.Slerp(playergraphic.transform.rotation, newRot, Time.time * RotSpeed);