Hi guys and welcome to part 14. In the last part we added a central mass of stars and stopped the stars colliding with each other. In this part we will bring everything together and get our spiral galaxy to work just like our disk galaxy. We will also fix the camera so we can pan to the edges of the galaxy again.

Warning: Brace yourselves for some serious cut and pasting as we tidy the code up! If you are a lazy person and just want things to work now:

Initialise the dictionary, set the galaxy seed, set galaxy view to true, create the planet data after creating starData and then add starData and starGO to the dictionary. If you don’t understand how to do that, stop being lazy and read on :P.

1. Viewing Solar Systems Again

Open up the Galaxy script. In the CreateGalaxyMethod we create a new dictionary for starsToObjectMap, set the random seed and set galaxyView to true. We will be doing this in our CreateSpiralGalaxy method too. Instead of copying and pasting, let’s create a new method called InitializeGalaxy. Make it void and don’t pass it anything. Cut and paste the three things I just mentioned into this method and then call InitializeGalaxy where they were. Then call it in the appropriate place in CreateSpiralGalaxy too.

InitializeGalaxy Method
Calling InitializeGalaxy in CreateGalaxy
Calling InitializeGalaxy in CreateSpiralGalaxy

Let’s also tidy things up a bit by creating two new void methods called CreateArms and CreateCentre. Cut and paste the correct for loops into these methods. Don’t forget to call them.

CreateArms has the i and j for loops
CreateCentre has the k for loop

We now have an issue in that percent, starsInCentre etc… can’t be passed into the new methods. To solve this we need to declare them in the Galaxy class instead (i.e. where we declare things like numberOfStars). This will make them common to the whole class and not just CreateSpiralGalaxy.

We also need to remove all the int and float types in the CreateSpiralGalaxy method.

Declaring in the Galaxy Class
Types Removed

We need to do this with starCount as well. It would also be a good idea to add starCount to InitializeGalaxy and set it to 0. Make sure all other “starCount = 0” are removed from the code (use the find tool to help you).

Declaring starCount
starCount in InitializeGalaxy

Ok so there’s a lot of tidying up going on in this part but trust me it will be worth it further down the line. Add three new methods called GalaxyMaths, CreateStarData and CreateStarObject.

Make GalaxyMaths void and don’t pass it anything. Make CreateStarData return a Star and pass it starCount (i.e. an int). Make CreateStarObject void and pass it a Star and a Vector3 (starData and cartPosition).

Setting Up GalaxyMaths
Setting Up CreateStarData
Setting Up CreateStarObject

In GalaxyMaths cut and paste our percent, starsInCentre etc… into this method and then call it.


Look how neat and tidy our CreateSpiralGalaxy looks. Only four lines of code :).

How about the other two methods I hear you ask. Well for the CreateStarData we cut and paste the line where we create starData. We also call our CreatePlanetData method before returning starData.


In the CreateStarObject method we cut and paste the line where we create starGO. We also add starData and starGO to starToObjectMap.


Now we call these in the appropriate places in CreateArms and CreateCentre.


Press play and you should be able to view solar systems again :). Sorry for this being a very long winded way to get things working again but like I said these improvements will be worth it in the future if we need to comeback and trouble shoot and cant remember what we did.

2. Fixing the Camera

So at the moment the camera does not pan all the way to the edge of our galaxy. This is because the camera is clamped to the maximumRadius value of the galaxy which we do not use in our spiral galaxy. We do not need to make any changes to our CameraController script however, all we need to do is in our GalaxyMaths method is set the maximum radius equal to our furthest possible star. 

But how do we know our furthest possible star? For this we take our distance calculation and sub in numberOfStars/numberOfArms when we calculate a square root. 

Calculating maximumRadius

The camera should now work correctly :).

While we are tweaking the camera it might also be a good time to change it so the camera cannot zoom too far out while in the solar system view. Open up the SolarSystem script. Declare two new floats called solarSystemZoom and storeMaxZoom. Make solarSystemZoom public and set the default value to 100. Keep storeMaxZoom private and set it equal to zero.

solarSystemZoom and storeMaxZoom

At the end of the CreateSolarSystem method, set storeMaxZoom equal to maxZoom then set maxZoom equal to solarSystemZoom.

Changing the Maximum Zoom Level

Now all we do is reset maxZoom in the DestroySolarSystem method.

Resetting maxZoom

Now when in the SolarSystem view the camera will not zoom out as much as when we are in the galaxy view.

This concludes this part. In the next part we will look at importing a name list for our stars.

Scripts_for_part_14 (zip file)

Part 15 Naming Stars