Wednesday, February 26, 2014

Continually rotate an object in Unity

In games it’s a very common task to rotate/spin an object continually, such as a coin, treasure box, health pack, weapons, etc. In Unity this is extremely simple. As with anything, there are multiple ways of doing it ex code or a particle system, but if we wan’t a quick script to do it, simply assign this script to your GameObject. Again, there are better ways. The quickest way may even be to just create a new animation, set a keyframe at 0 seconds, move to 1 second and rotate the object to 360 degrees and a new keyframe will be logged. From a performance standpoint, thats typically the best _but_ this script below can by dynamically applied to any object.
    private int _rotationSpeed = 90; //degrees per second = 4 seconds for a revolution
    void Update()
    {
        //Since deltaTime is the elapsed time since the last frame, 
        //let’s assume its one second. 1 * 90 = 90 degrees per second. 
        //Simple, right? Rotate’s parameters is (x,y,z, coordinate system)
        transform.Rotate(0, _rotationSpeed * Time.deltaTime, 0, Space.World);
    }
Voila - A spinning object!

There is however a basic optimization we can do here. Even though it seems that this is pretty simple, we can cache our transform. This prevents having to cross over from the 'managed' type of code that c#/mono/.net provides across boundaries into the native code from the c++ based engine that Unity wrote. This type of transition is slow so we can optimize it a little bit as follows by getting a reference to it upon start which is then stored in our c# code. We can then access it without having to do an extra hop into the native c++ code that the Unity engine runs on.
    private int _rotationSpeed = 90; //degrees per second = 4 seconds for a revolution
    private Transform _transform;
    void Start()
    {
     //cache the transform in our managed code side
      _transform = transform;
    }
    void Update()
    {
        //Since deltaTime is the elapsed time since the last frame, 
        //let’s assume its one second. 1 * 90 = 90 degrees per second. 
        //Simple, right? Rotate’s parameters is (x,y,z, coordinate system)
        _transform.Rotate(0, _rotationSpeed * Time.deltaTime, 0, Space.World);
    }
treasure spin

4 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This is really neat. I have some free time right now, so I am thinking - why not try these out? Your posts are always full with interesting ideas, thank you a lot for sharing them with us.

    ReplyDelete
  3. Hello. Thanks for the interesting article and tips.
    You're right. In a college or university, a student not only gets new knowledge, but also begins to better understand himself - his goals, desires, interests and talents.
    So, many students, having learned to write good examples of thesis, college application essay https://college-pages.com/college-application-essay/, etc., open up a literary, and maybe even journalistic, talent.
    Other students understand that they are not bad at writing programs. And who knows, maybe soon we'll see a new operating system created by a group of students from some university.
    So, the main thing is to understand what a person is strong and what he likes and do not be afraid to realize it. And then it does not matter, at school you study or at the whole university - one your idea can benefit millions ...

    ReplyDelete

Note: Only a member of this blog may post a comment.