Hi guys and welcome to part 10. In the last part we finally finished our basic camera controls. In this part we will make our solar systems look more interesting by adding orbital lines to our planets.
Firstly I have made a basic sprite to use as our orbital path which you can download here:
orbitsprite (zip file)
Create a new folder in the assets folder called Sprites and add the sprite to it. In the inspector change the sprite from a texture to a Sprite (2D and UI). Leave all the other settings the same and click apply.
We need to make a Prefab using this sprite so in Unity right click in the hierarchy and add a new sprite (under 2D Object in the menu). Rename it Orbit Sprite, rotate it by 90 in the X axis and add our orbit picture to the sprite box under the Sprite Renderer.
Now make a new folder in the asset folder and call it prefabs. Drag the Orbit Sprite object from the hierarchy into this new folder to create the prefab.
Now delete the Orbit Sprite object in the hierarchy as we do not need it in our scene anymore.
We have a sprite ready to use now but how do we add it to our solar system? First we need to let our SolarSystem script know which sprite to use. As we made our sprite a Prefab we add a public GameObject to our script and then assign our prefab in the inspector. Call it OrbitSpritePrefab.
All our planets and stars are currently created using the SpaceObjects script. As the orbit paths are an extension of these objects lets create a new method in this script called CreateOrbitPaths. Make it public static and return a GameObject. It will need to be passed a GameObject (our orbit prefab). It would probably be a good idea to keep track of which object the orbit belongs to so also pass it a string. The sprite size will need to be adjusted depending on the distance the planet is away from our star so pass it an int. Lastly we will want it to be a child of the Solar System Manager (so the orbit is deleted when we return to our galaxy view) so pass it a transform (make this and optional pass and make it equal to null like our create sphere object).
The first thing this method should do is to create our orbit objects so declare a GameObject called orbit and use Object.Instantiate to create it from the passed GameObject. We may as well add the return statement now so add that too.
Before we return orbit we need to set its name, size and parent.
Back in the SolarSystem script we can now call CreateOrbitPath after we create the planet objects. We pass it orbitspriteprefab, planet.name + ” Orbit”, i + 1 (because i starts at zero) and this.transform.
Now press play and the solar systems look a little bit more interesting with the planet orbits added!
I made the orbit sprite black and white so that you could customise what colour your orbit sprite is. To do this just select the prefab in the assets folder and change the colour in the inspector :). You can also adjust the alpha here to make the paths more or less transparent.
Note: The sprite is not perfect as the ring gets wider the larger the orbit. However it will do for now and I don’t know about you but I kind of like this look :).
This concludes this part. In the next part we will add a selection icon to our galaxy view so it is easy to see which star we have our mouse over.
Scripts_for_part_10 (zip file)