Rotating satellites

If you have noticed, in our package we have some planets with some circles around them. Even if the whole planet can be driven by FallingScript, we can create satellites as children of the planet, rotating around it. So, create a prefab for the planet with the circle, and attach FallingScript. Then, as a child (or children if you want more than one) attach satellites to the RotatingSatellite script we are going to create. As a result, we will have a planet with satellites orbiting around, as shown in the following image:

Now, let's create the aforementioned script (RotatingSatellite), and open it. Once again, we need the speed variable:

    public float speed = 3f; 

Then, we need a variable to store the radius of the orbit. By adjusting this value, designers are able to tweak how far the satellite orbits around it (or adjusting to the circle's radiuses):

    public float radius = 1f; 

For the motion, we just need to follow a circular path. If you are familiar with trigonometry, this is just having a cosine on the x and the sine on the y; as arguments of these trigonometry functions, we need the time scaled by the speed. Then, it's just a matter of assigning the variables to the new position. Of course, the position needs to be in local space, since we are assigning the new position with respect to the planet:

   void Update () { 
        //Calculate the new x and y of the circular motion 
        var x = radius * Mathf.Cos(speed * Time.time); 
        var y = radius * Mathf.Sin(speed * Time.time); 
 
        //Assign the new position to the object transform 
        transform.localPosition =  new Vector3(x, y, 0f); 
   } 

Save the script, and enjoy satellites orbiting around your planets.

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

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