make rotation smooth and instant

hi, im new to unity and have a problem with making a cube rotate,

this is the code im using and its attached to a cube:

Vector3 myRotation = new Vector3(0,0,0);
bool rotate = false;
void Update ()
{

	if (rotate)
	{
		transform.Rotate(myRotation);
	}
	switch(Input.inputString)
	{
		case "d":
			rotate = true;
			myRotation.y = 2;
		break;
		
		case "a":
			rotate = true;
			myRotation.y = -2;
		break;
		
		case "":
			rotate = false;
			myRotation.y = 0;
		break;
		
		case default (string):
			rotate = false;
			myRotation.y = 0;
		break;
	}
	
	if (rotate)
	{
		transform.Rotate(myRotation);
	}
}

the problem is that the cube doesnt instantly, or smoothly, rotate but instead waits for about a second and then sdtarts rotating.

can anyone help me with getting this to rotate smoothly?

You know how when you hold down a key, it prints once, then a delay and it prints like 20 a second? That’s what Input.inputString is reading. It looks like: “d”, “”, “”, “”, “”, “”, “d”, “”, “”, “d” … . It’s more for text entry. Lots of ways to fix the problem, once you know that.

You might convert to testing if(Input.getKey("d")) (read the Input section of the scripting manual.) It’s specially made to be true every frame “d” is down.

Thanks for your reply :slight_smile:

When I started I did use the input.getkeydown, onyl I thought it would be more efficient if I could use a switch case.

otherwise I’d have to make and if statement for getkeydown.D, then an else if for getkeydown.A and a nother if statement for if they were released :confused:

so should I just do that, or is there a more efficient way?

Thanks.