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

5 comments:

  1. Thanks for the information.

    SyonIndia is a Software Development, Website development and SEO company in Bhubaneswar. We offer professional software development services in Bhubaneswar India. Our developers are highly skilled, advance and expert to execute things up to the mark and are efficient enough to cope up with new platforms. We are the leading software development company in Bhubaneswar.
    http://syonindia.com

    ReplyDelete
  2. Excellent blog !!! Actually i was searching this and now i found the solution. Thank you so much.
    Webzin Infotech

    ReplyDelete
  3. You blog is eye-catching. I get pleasure from it.Redure Webtechnologies Provides Web Design, Web Development, SEO, SMO, E-mail and SMS Service Provider company. We are providing value to business, realizes the importance of customization of web services with respect to the client's requirement.
    Website Designing and Development Services

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. "In games it’s a very common task to rotate/spin an object continually, such as a coin, treasure box, health pack, weapons, etc." - Can't but agree more! All mobile app and website developers and designers should always keep it in mind.

    ReplyDelete