Auto Rotating Script

I need help creating an auto-rotate script around three different objects from three different angles. I unfortunately do not know where to begin. Any and all help is greatly appreciated

1 Answer

1

okay, lets take it one step at a time.

Auto rotate

This can be archived by putting a Rotate() call in each frame.

In Unity we have the Update event for things that happens over and over.

The thing we need to be able to rotate is a GameObject’s transformation matrix. This is called “transform” for short.

Now, how to do this?

Make a script. Lets make a C# script and call it “Rotator.cs”

Open it in MonoDeveloper

In the Update place this:

  void Update()
  {
      transform.Rotate(
           xSpeed * Time.deltaTime,
           ySpeed * Time.deltaTime,
           zSpeed * Time.deltaTime
      );
  }

Now what it does is that it rotates the GameObject you have dragged/added this script to, with the 3 speeds mentioned. The variable Time.deltaTime, is a value that tells how much time went since the last Update and this will make your objects rotate/move with the same speed, no matter how many frames per second you run your application.

You will also need to define the speed (x,y,z) variables and to be able to have different speeds on each angle and object, I suggest you make them “public” this keyword will make them show up in the Unity Editor in the Inspector panel, when you select the GameObject where this script is.

So, add this in the top of the Rotator.cs file:

public float xspeed = 0.0f;
public float yspeed = 0.0f;
public float zspeed = 0.0f;

Now you can add this script to ANY object you might want to auto-rotate. After adding it, change the default speed to what ever you need for your “galaxy”/solar system/rotating thingy… :o)

Hope you have fun with it.

Cheers.

Thank you so much for this amazing response. I do have a few questions and problems though: Is there a way I can convert this to a javascript? I currently have part of the function scripted; the script is here: http://db.tt/R27MowTP . The problem I'm experiencing is that it doesn't rotate around the object. If needed, I can create a video of it so you can have a better grip of the problem.