How to rotate object to original starting value?

Hi guys!
So Im trying to make a ball maze game and having problems with my code.
Right now i have built controls that will move my base plane, the plane that the ball rolls on with this code and it seems to work out.

function Update () {

	if (Input.GetKey ("a")){
		print ("a key was pressed");
		transform.Rotate(Time.deltaTime*20, 0, 0);}
			
			if (Input.GetKey ("a") && Input.GetKey ("s")){
				transform.Rotate(Time.deltaTime*10, 0, Time.deltaTime*10);}
			else if (Input.GetKey ("a") && Input.GetKey ("w")){
				transform.Rotate(Time.deltaTime*10, 0, -Time.deltaTime*10);}
	
	else if (Input.GetKey ("d")){
		print ("d key was pressed");
		transform.Rotate(-Time.deltaTime*20, 0, 0);}
			
			if (Input.GetKey ("d") && Input.GetKey ("s")){
				transform.Rotate(-Time.deltaTime*10, 0, Time.deltaTime*10);}
			else if (Input.GetKey ("d") && Input.GetKey ("w")){
				transform.Rotate(-Time.deltaTime*10, 0, -Time.deltaTime*10);}
	
	else if (Input.GetKey ("w")){
		print ("w key was pressed");
		transform.Rotate(0, 0, -Time.deltaTime*20);}
			
			if (Input.GetKey ("w") && Input.GetKey ("d")){
				transform.Rotate(-Time.deltaTime*10, 0, -Time.deltaTime*10);}
			else if (Input.GetKey ("w") && Input.GetKey ("a")){
				transform.Rotate(Time.deltaTime*10, 0, -Time.deltaTime*10);}
	
	else if (Input.GetKey ("s")){
		print ("s key was pressed");
		transform.Rotate(0, 0, Time.deltaTime*20);}
		
			if (Input.GetKey ("s") && Input.GetKey ("d")){
				transform.Rotate(-Time.deltaTime*10, 0, Time.deltaTime*10);}
			else if (Input.GetKey ("s") && Input.GetKey ("a")){
				transform.Rotate(Time.deltaTime*10, 0, Time.deltaTime*10);}

Problem is that I don’t know how to move the plane back to it´s original position (0,0,0).
I want this to happen when I don’t push any of my keys.
I had this at the end of my code:

else {
transform.rotation = Quaternion.identity;}

But that didn´t work out so well for me, It seems like the else statement runs all the time as well and the transform happens too fast, can i implement time.delta.time maybe?

//Cheers Mathias

Firstly, you’ve got a lot of unnecessary checks in that. Every time you check something like this:

 if (Input.GetKey ("s") && Input.GetKey ("d")){

You could just put it inside the previous check (which checks if the s key is down in this example), and just check the d key, so instead of:

else if (Input.GetKey ("s")){
print ("s key was pressed");
transform.Rotate(0, 0, Time.deltaTime*20);}
 
if (Input.GetKey ("s") && Input.GetKey ("d")){
transform.Rotate(-Time.deltaTime*10, 0, Time.deltaTime*10);}
else if (Input.GetKey ("s") && Input.GetKey ("a")){
transform.Rotate(Time.deltaTime*10, 0, Time.deltaTime*10);}

You’d have:

else if (Input.GetKey ("s"))
{
    print ("s key was pressed");
    transform.Rotate(0, 0, Time.deltaTime*20);
 
    if (Input.GetKey ("d"))
    {
        transform.Rotate(-Time.deltaTime*10, 0, Time.deltaTime*10);
    }
    else if (Input.GetKey ("a"))
    {
        transform.Rotate(Time.deltaTime*10, 0, Time.deltaTime*10);
    }
}

This is not only faster, but will solve your other problem too. The reason your else statement didn’t work was because it was only checking if the last 2 statements were not true, so it would always run as long as S and either A or D wasn’t down. By chaining ALL the ifs into one if-else chain, you can add the final else statement which takes the “not” case of all of them.

My code now works thanks to you sir :slight_smile:

var speed = 0.1;
function Update () {

if (Input.GetKey (“a”))
{
print (“a key was pressed”);
transform.Rotate(Time.deltaTime*20, 0, 0);

if (Input.GetKey ("s"))
{
    transform.Rotate(Time.deltaTime*10, 0, Time.deltaTime*10);
}
else if (Input.GetKey ("w"))
{
    transform.Rotate(Time.deltaTime*10, 0, -Time.deltaTime*10);
}

}

else if (Input.GetKey (“d”))
{
print (“d key was pressed”);
transform.Rotate(-Time.deltaTime*20, 0, 0);

if (Input.GetKey ("s"))
{
    transform.Rotate(-Time.deltaTime*10, 0, Time.deltaTime*10);
}
else if (Input.GetKey ("w"))
{
    transform.Rotate(-Time.deltaTime*10, 0, -Time.deltaTime*10);
}

}

else if (Input.GetKey (“w”))
{
print (“w key was pressed”);
transform.Rotate(0, 0, -Time.deltaTime*20);

if (Input.GetKey ("d"))
{
    transform.Rotate(-Time.deltaTime*10, 0, -Time.deltaTime*10);
}
else if (Input.GetKey ("a"))
{
    transform.Rotate(Time.deltaTime*10, 0, -Time.deltaTime*10);
}

}

else if (Input.GetKey (“s”))
{
print (“s key was pressed”);
transform.Rotate(0, 0, Time.deltaTime*20);

if (Input.GetKey ("d"))
{
    transform.Rotate(-Time.deltaTime*10, 0, Time.deltaTime*10);
}
else if (Input.GetKey ("a"))
{
    transform.Rotate(Time.deltaTime*10, 0, Time.deltaTime*10);
}

}

	else {
		transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion(0,0,0,1), Time.time * speed);
		}

}

However, I decided to change my controller to move my ball instead of tilting the ground.
pushing its rigid body by force, and since tha if statements worked now this was not a problem at all :smiley:
Think this will be a fun game, thank you very much.

Since Im new I don´t know what to do with this forum thread now?
Am I suppose to accept the answer as correct?

This was the code that answered my question " How to rotate an object to its original, in this case 0,0,0 value over time.

transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion(0,0,0,1), Time.time * speed);