# Let’s Make a 4X Space Game with Unity – Part 13 Spiral Galaxies 2

Hey guys and welcome to part 13. In the last part we started to add spiral galaxies to our game. In this part we will keep going with this, adding more stars to the centre and making sure no stars collide with each other.

First just add a new int called starCount and set it to 0. Add this before we start making the stars in our CreateSpiralGalaxy method. Then where we name the stars change the name of the stars to “Star ” + starCount. This will make our naming much simpler. Increase starCount each time starData is created.

Let’s add the central mass of stars. Open up the Galaxy script. We need to know how many stars are going to be in the centre of the galaxy. Lets declare a new public int called percentageStarsCentre and set the default to 25. As the name suggests this will be a percentage of the total number of stars. For this reason we can use a nifty feature of Unity to clamp this value between 0 and 100 – [Range()] PropertyDrawer. This changes the input in the editor to a slider instead of a text box (see picture below).

Note: By doing this we can also in the future have only one galaxy constructing method (as disk galaxies will have 100% of the stars in the centre). We won’t be doing this now to keep clarity in the tutorials, however it will be something we can consider later on.

In our CreateSpiralGalaxy method we need to convert this value to a percentage. All we do for that is divide it by 100. This will be a new float called percent. Add this to the very top of the method as we need to work out our centre stuff before we create the arms. The reason for this will become clear in a moment.

Note: Make sure you include the f after the 100. If you miss this out the number returned will always be 0 or 1 as the computer will convert it to an int.

Now that we have this we can calculate the number of stars in the centre as a float by multiplying percent by numberOfStars. We will need to round this to an int like we did with the arms in the last tutorial. Call these starsInCentre and starsInCentreRounded.

We need to make a couple of tweaks to our starsPerArm and difference calculations. We need to take starsInCentreRounded from the number of stars before dividing by numberOfStars in starsPerArm and we need to subtract starsInCentreRounded in our difference calculation.

All our maths is done so we can now start a for loop to spawn our central stars. We can put this before or after we spawn our arms but I have put it after. The limit of the for loop will be starsInCentreRounded + difference. This will ensure we always have the correct number of stars in our galaxy by adding or subtracting any discrepancies created from rounding and make sure our arms are equal.

Now we just follow the motions, create a new star called starData, get a random position for it and create a game object.

Note: you may have noticed I haven’t included a lot of things like creating the planet data or checking for collisions. The reason for this is because until we get a fully working spiral system I do not want to touch the other scripts to change them (for example change the SolarSystem script to call our CreateSpiralGalaxy method instead of CreateGalaxy). I also wanted to drip feed the information so that if anyone came in on these tutorials rather than the original galaxy tutorials (parts 1 – 6) it would make sense to them :).

2. Collisions

We can add our collision checks now however I want to tweak the system we had before. Create a new method called CheckCollisions. Put this in our PositionMath class as it seems to make more sense to put it here then Galaxy :). Make it public static, return a bool, pass it a float (this will be our minDistanceBetweenStars value) and a vector3 (cartPosition).

Declare a new bool called collision, set it equal to false and copy and paste the line of code we used in CreateGalaxy to create positionCollider. Return collision.

Before we return collision we need to add an if statement which checks if the lenth of positionCollider and sets collision to true if it is greater than 0.

Back in our CreateSpiralGalaxy we can now call this method to set a bool called collision before we create starGO. We then make an if statement to check collision is not true and only create starGO in that case. Else we try again. Do this for both the arms and the centre.

Let’s add failCount for both just like we did for the CreateGalaxy method just so we don’t accidentally create infinite loops. Remember to do this for both the arms and the centre.

Don’t forget to increase starCount either.

Hit play and now none of the stars should be touching.

This concludes this part of the tutorial. In the next part we will get all our functionality back so we are able to view the solar systems again. We will also fix the camera so we can scroll all the way to the edges again :).

scripts_for_part_13 (zip file)

Part 14 Spiral Galaxies 3