Signup/Sign In

Solar System Animation Script

After bringing the spheres which will act like planets and giving these planets light through sun, and then setting up the universe-skybox for the entire scene, now i's time to rotate and revolve the planets and their natural satellite. For rotation and revolution you have to write a C# code, and apply it onto your game objects. So you have to apply some required steps to implement the rotation and revolution. Let's start working on the motion part.


Stepwise Representation of Rotating and Revolving the Spheres

Follow the below mentioned steps to apply motion in your game.

  1. Go to Asset Menu under Project Window Pane and add a separate Folder. Inside that folder click Create > C# Script
  2. Now you have the following code:
    
    using UnityEngine;
    using System.Collections;
    
    public class RotateAround : MonoBehaviour {
    	public Transform target; 	// the object to rotate around
    	public int speed; 	// the speed of rotation
    	void Start()
    	{
    		if (target == null) 
    		{
    			target = this.gameObject.transform;
    			Debug.Log ("RotateAround target not specified. Defaulting to parent GameObject");
    		}
    	}
    	// Update is called once per frame
    	void Update () 
    	{
    		// RotateAround takes three arguments, first is the Vector to rotate around
    		// second is a vector that axis to rotate around
    		// third is the degrees to rotate, in this case the speed per second
    		transform.RotateAround(target.transform.position,target.transform.up,speed * Time.deltaTime);
    	}
    }
    
    
  3. Save the C# code and compile it.
  4. Now select the Sun (biggest 3D sphere) and go to Inspector Pane > Drag and Drop your C# script onto the Sun-sphere.
  5. In the inspect Pane, under Rotate-Around Script, change the value of Speed attribute from 0 to 4, and the target value will be the 'Sun' object
  6. Now in the Hierarchy window, drag the Moon-sphere game object on to the Earth-object, you will see that the Earth got a sub-tree and inside it the moon-sphere object will reside. This eventually creates a relation between the earth and the sun. So when the earth revolves around the sun, the moon also revolves around the sun, under earth as a sub part of earth
  7. Now again use the same C# code and drag and drop it onto the Earth-sphere game object. This time use the Rotate-Around script will appear on the Inspector pane twice. Once for the earth itself to rotate around its own axis (in this case you have to drag and drop the C# script on to earth game object and then the Earth object from Hierarchy Window pane to 'Target' attribute under Inspector-Pane in Rotate-Around script section) and the other on the Earth game object again drop the C# rotateAround script and then drag and drop the Sun object from Hierarchy Window Pane to 'Target' attribute of Rotate-Around script under Inspector pane
  8. Set the 'Speed' attribute value to 4 or 5

    Solar System Animation Script


  9. Again, for the moon game object, you have to drag and drop the C# script twice, one for the moon itself and other for the earth upon which the moon will revolve. Like the previous step, drag and drop the C# code on to the moon once and from the Hierarchy Window Pane, drag and drop the moon game object to the Rotate-Around script's 'Target' attribute under Inspector Pane. Again, drag and drop the C# script on to the moon game object and then go to Inspector Pane. From the Hierarchy Window Pane, drag and drop the Earth game object and place it on to another 'Target' attribute of Rotate-Around script under Inspector Pane. Also set the target Speed attribute to 10 in case of moon's rotate-around script

And you are done. Your animation will start working if you have properly followed the steps mentioned above