Let’s Make a 4X Space Game with Unity – Part 14 Spiral Galaxies 3

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.

part014pic001
InitializeGalaxy Method
part014pic002
Calling InitializeGalaxy in CreateGalaxy
part014pic003
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.

part014pic004
CreateArms has the i and j for loops
part014pic005
CreateCentre has the k for loop
part014pic006
CreateSpiralGalaxy

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.

part014pic007
Declaring in the Galaxy Class
part014pic008
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).

Part014Pic009.JPG
Declaring starCount
part014pic010
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).

part014pic011
Setting Up GalaxyMaths
part014pic012
Setting Up CreateStarData
part014pic013
Setting Up CreateStarObject

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

Part014Pic014.JPG
GalaxyMaths
Part014Pic015.JPG
CreateSpiralGalaxy

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.

Part014Pic016.JPG
CreateStarData

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

Part014Pic017.JPG
CreateStarObject

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

part014pic018
CreateArms
part014pic019
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. 

Part014Pic020.JPG
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.

Part014Pic021.JPG
solarSystemZoom and storeMaxZoom

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

Part014Pic022.JPG
Changing the Maximum Zoom Level

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

Part014Pic023.JPG
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

 

 

 

 

Advertisements

4 thoughts on “Let’s Make a 4X Space Game with Unity – Part 14 Spiral Galaxies 3

  1. I’ve just finished this part and i can’t barely wait to see what’s coming next. You did a really good job and i hope you will continue your tutorials in the coming months. I have just one question about the gameplay of your 4X, will it be turn-based or real-time ? And do you plan to explain how to do both ?

    See you soon on next parts!

    Like

    1. Hi Aymeric thank you for you’re great feedback 🙂 I put a lot of time into these tutorials and it’s nice to know they are appreciated.

      To answer your question, I still haven’t decided. I am leaning towards turn based. Which one would you be most interested in?

      Like

  2. Hi,

    As a big fan of Master of Orion and Endless Space I appreciate having time to plan my next move especially in a genre like 4X where there’s a lot of information and things to do.

    So, i’m totally leaning toward Turn-based, cause i don’t like to rush and i really suck when gameplay is about playing quicker than the AI.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s