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.

1 Answer

1
  1. this is perfectly, flawlessly, amazingly efficient

  2. note that it is extremely unlikely you want to rotate it by “1” every frame (frames happen up to 100s of times per second)

  3. it is very likely you want to do something like .Rotate( 0, Time.deltaTime, 0)

  4. you can find endless examples on here about the details of this, eg

search in google on “answers.unity3d.com Rotation Time.deltaTime” to find literally 100s of such answers.

more tricky …

  1. there is no “buffer” whatsoever

  2. 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

  3. 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

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.