- Home /
Constantly rotate a gameobject around y axis.
I want to keep rotating a gameobject throughout the scene, Currently what code i am using is..
void Update () {
//rotate the current item
items[currentItem].transform.Rotate(0,1,0);
}
However, I think this is not efficient as update gets called in between the object is rotating so there is a huge buffer of rotation building up and the object keeps on rotating even when the gameobject (items[currentItem]) changes. How can i inexpensively keep rotating a object and make it stop as soon as it is not getting any more rotation commands.
Answer by Fattie · Mar 20, 2013 at 12:49 PM
this is perfectly, flawlessly, amazingly efficient
note that it is extremely unlikely you want to rotate it by "1" every frame (frames happen up to 100s of times per second)
it is very likely you want to do something like .Rotate( 0, Time.deltaTime, 0)
you can find endless examples on here about the details of this, eg
http://answers.unity3d.com/questions/52790/constant-rotation-to-an-object.html
search in google on "answers.unity3d.com Rotation Time.deltaTime" to find literally 100s of such answers.
more tricky ...
http://answers.unity3d.com/questions/14527/rotate-90-over-time-on-mousedown.html
there is no "buffer" whatsoever
when you change "currentItem", it will instantly stop rotating the previous "currentItem" and begin rotating the new "currentItem". If it does not: you have made a mistake somewhere else. To solve that problem, add a debug line, Debug.Log .. etc
regarding the question of "how to do this only when a button is being held down"
Essentilally, you add code like this:
function Update()
{
if the button is being held down then
items[currentItem].transform.Rotate(0,1,0);
}
regarding how to detect if a button is being held down, search on here for 1000s of answers
or glance at the doco
http://docs.unity3d.com/Documentation/ScriptReference/Input.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButton.html
Hope it helps !!!
Hi, Thanks for the detailed response. I am not using mouse for interaction, i am using kinect and detecting hand movement and current item changes when the hand is over other item. but the previous item keeps on rotating .
(1) I applied the Time.deltaTime now, seems to be working fine now. Thanks :).
(2) Ok, i will look for the problem, no need for the all caps rage .
(3) The mentioned code as such has nothing to do with the kinect, i meant to say that using mouseclick is not a option.
Thanks for all the help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Double Tap Not Working 1 Answer
Transform [] = all children of this game object ? (Spawnpoints) 3 Answers
Send array of transforms via RPC. [C#] 1 Answer
How to set a Transform variable with code (C#) 3 Answers
Perpendicular forward :-) 0 Answers