How to do it...

To use the Rotate() function for animating objects, follow these steps:

  1. Import our windmill to Unity. The model should contain three objects: WindmillBaseWindmill (parented to WindmillBase), and WindmillWings (parented to Windmill).
  2. Place the model in the scene.
  3. Create a new C# script and name it ScriptRotation.cs. In this script's Update() function, we use only one method: transform.Rotate(). We have a series of public variables to be able to control the rotation speed and axis. The Rotate() method can rotate an object in the World or Self (local) space. We have a public Space rotationSpace variable to be able to control it from the Inspector:
      transform.Rotate(rotateAxis * rotationSpeed * 
                  Time.deltaTime, rotationSpace); 
  1. Assign the script to the Windmill game object.
  2. Set the Rotation Space to World, and the Rotation Axis to (0, 1, 0). This means our object will rotate in the global Y axis (the up vector). Set the Rotation Speed to your liking (it's in degrees per second; a value of 45-90 should be OK).
  3. Assign the same script to the WindmillWings game object.
  4. Set the Rotation Space to Self and the Rotation Axis to (0,0,1). This means the object will rotate around its local Z axis (the forward vector of the object). Again, set the Rotation Speed to your liking.
  5. Play the game and observe the effect. The Windmill game object rotates in the global Y axis and the WindmillWings game object rotates around its local Z axis and follows the Windmill's Y axis rotation (because of the hierarchy setup).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.191.233