What am i doing wrong?

The following code is on an object, I’m useing it as a player control. Well makeing it, And i want the object if up or down key is pressed to smoth rotate back to 0,0,0,0. This line is the one giveing me problems.

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion(0,0,0,0), Time.deltaTime * 50);

It works to reverting it to 0,0,0,0 but if i change the Time.deltaTime * 50 to something slower like 25 it dosn’t work at all. Whats going on?

function Update () 
{
	//Player Controls
	if(Input.GetKey ("up")) 
	{
        transform.Translate(Vector3.up * Time.deltaTime * speed, Space.World);
      	transform.Rotate(Vector3(100,0,0) * Time.deltaTime, Space.Self);
      	if(transform.localEulerAngles.x >= 30  transform.localEulerAngles.x <= 60)
      	{
      		transform.localEulerAngles.x = 30;
      	}
    }
   	else if(Input.GetKey ("down")) 
	{
        transform.Translate(Vector3.down * Time.deltaTime * speed, Space.World);
      	transform.Rotate(Vector3(-100,0,0) * Time.deltaTime, Space.Self);
      	if(transform.localEulerAngles.x <= 330  transform.localEulerAngles.x >= 300)
      	{
      		transform.localEulerAngles.x = 330;
      	}
    }
    else 
    {
    	transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion(0,0,0,0), Time.deltaTime * 50);
    }
    
    if(Input.GetKey ("right"))
    {
    	transform.Translate(Vector3.right * Time.deltaTime * speed * 0.8);
    }
    if(Input.GetKey ("left"))
    {
    	transform.Translate(Vector3.left * Time.deltaTime * speed * 0.8);
    }
    if(Input.GetKey ("space"))
    {
    	Debug.Log("Shoot");
    }
}

Are you getting any errors?

NO errors, Like i said it works it i use 50 or higher, But anything lower and nothing happens.

I’m not sure what is wrong. I tested your script and you’re right, it doesn’t work with anything less than 50 which is strange because in the Scripting Reference example the speed is 0.1. I also tried to use Lerp instead of Slerp but I just got errors. I’ll do a little more tinkering but I don’t see anything wrong with it, granted though I’m not an expert on Quaternions.

Change Quaternion(0, 0, 0, 0) to Quaternion.Euler(0, 0, 0).

Edit: See below.

Ah! I’ve got it! Here is the new code.

var speed : float = 1;

function Update ()
{
	//Player Controls
	if(Input.GetKey ("up")) 
	{
        transform.Translate(Vector3.up * Time.deltaTime * speed, Space.World);
      	transform.Rotate(Vector3(100,0,0) * Time.deltaTime, Space.Self);
      	if(transform.localEulerAngles.x >= 30  transform.localEulerAngles.x <= 60)
      	{
      		transform.localEulerAngles.x = 30;
      	}
    }
   	else if(Input.GetKey ("down")) 
	{
        transform.Translate(Vector3.down * Time.deltaTime * speed, Space.World);
      	transform.Rotate(Vector3(-100,0,0) * Time.deltaTime, Space.Self);
      	if(transform.localEulerAngles.x <= 330  transform.localEulerAngles.x >= 300)
      	{
      		transform.localEulerAngles.x = 330;
      	}
    }
    else 
    {
    	transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, Time.deltaTime * 1);
    }
    
    if(Input.GetKey ("right"))
    {
    	transform.Translate(Vector3.right * Time.deltaTime * speed * 0.8);
    }
    if(Input.GetKey ("left"))
    {
    	transform.Translate(Vector3.left * Time.deltaTime * speed * 0.8);
    }
    if(Input.GetKey ("space"))
    {
    	Debug.Log("Shoot");
    }
}

It works now. Make sure to keep the “50” number low, no higher than 10 or it will rotate too fast to see.

I changed this:

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion(0,0,0,0), Time.deltaTime * 50);

to this:

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, Time.deltaTime * 1);

I’m no expert on them either, Lerp uses Vector3’s though, Tried RotateTowards as well with no visible luck. No error’s as well on that. but no luck.

Thanks so much, Now I have to look into what identity is exacly to know what it was missing from the prevois statment so maybe i don’t make this mistake agian. Also agian thank you, Been racking my brain for like 2 hours on that one line.

http://unity3d.com/support/documentation/ScriptReference/Quaternion-identity.html

This talks about Quaternion.identity. It aligns the objects axis with the world’s axis.

http://unity3d.com/support/documentation/ScriptReference/Quaternion-identity.html

This talks about Quaternion.identity. It aligns the objects axis with the world’s axis.

Woops! Sorry I posted two of the same.

You could delete your post by clicking on edit then delete posts. :smile: