- Home /
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?
Answer by Owen-Reynolds · Oct 10, 2011 at 02:18 PM
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.
Answer by IronFurball · Oct 10, 2011 at 07:30 PM
Thanks for your reply :)
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 :/
so should I just do that, or is there a more efficient way?
Thanks.
Can avoid checking for keyUps with clever program$$anonymous$$g (rotate if "d"d is pressed, otherwise don't.) Getting it working is #1. Once you get some experience, and know which part is slowing you down, you can try and rewrite it faster.
Also, try to avoid posting comments as answers.
Your answer
Follow this Question
Related Questions
90 Degree Smooth Rotate On Button Press 1 Answer
Turn page, smooth rotate 1 Answer
OnTriggerEnter smooth Rotate 1 Answer
smooth rotate help 1 Answer
Rotate camera around object smoothly 2 Answers