Let’s Make a 4X Space Game with Unity – Part 11 Star Selection Icon

Hi guys and welcome to part 11. In the last part we added orbit paths for our planets to make our solar systems look more interesting. In this part we will add a selection icon to our galaxy and solar system views. This will make it easier for the player to see which object their mouse is over before they select it.

I have created a basic sprite for our selection box and you can download it here:

selectionbox (zip file)

Add this to our sprites folder and change the settings so it’s not a texture anymore (like we did with the Orbit Sprite in the last part). Change the pixels per unit to 64.

Part011Pic001.JPG
Import Settings

Drag the sprite into the hierarchy and then from the hierarchy to the Prefabs Folder to create a Prefab. Rename the prefab to Selection Icon. Just like the Orbit Sprite in the last part I created the Selection Icon in black and white so you can customise the colour so feel free to change the colour on the prefab :).

Part011Pic002.JPG
Selection Icon in the Inspector

Open the Galaxy script. We will be adding the prefab to this script as a public GameObject called selectionObject.

Part011Pic003.JPG
Declaring selectionObject
Part011Pic004.JPG
Assigning the Prefab

Let’s add a new void method to the Galaxy script called CreateSelectionIcon (nothing passed to it). We set selectionIcon equal to a newly instantiated selectionIcon to create the GameObject in the game scene. We also want it to be larger than the stars/planets. Later we will change the size of planets so we will need a dynamic solution. However for now we can just increase the localScale of selectionIcon by 2.5 times (this seems like a nice visual size to me). Lastly we want to disable the object as we don’t want it to be visible at the start of the game.

Part011Pic005.JPG
CreateSelectionIcon Complete

 Call CreateSelectionIcon after SanityChecks is called in the Start method.

part011pic006
Calling CreateSelectionIcon

We now have a selection icon in the game but we can’t yet see it. We need one more method in our Galaxy script and make a couple of tweaks to our SolarSystem script before we can see the object.

Let’s start with the new method. In the Galaxy script add a new method called MoveSelectionIcon. Make it public, void and pass it a RaycastHit called hit. Re-enable the object then set the position to the RaycastHit transform.

Part011Pic007.JPG
MoveSelectionIcon

Open the SolarSystem script. We need to change our if statement in the Update method. At the moment it requires both the mouse to be over an object AND the mouse button down. We need to change this so it only requires the mouse to be over an object. We then make another if statement nested in the first one requiring the left mouse click and galaxy view being true. We then move all our code to this new if statement.

part011pic008
Tweaking Update

We now call MoveSelectionIcon before the second if statement passing it hit.

Part011Pic009.JPG
Calling MoveSelectionIcon

After both if statements are over add an else condition and disable the selectionObject

Part011Pic010.JPG
Disable Selection Icon

Press play. Notice how the object appears and disappears when you move the mouse over stars and planets. There is a problem however; It doesn’t face the camera. We can fix this by making a tweak to our CameraController script. Declare a new public static Quaternion called currentAngle. In the ResetCamera method add a new line which sets currentAngle to the rotation objects rotation (after it has been set).

Part011Pic011.JPG
Declaring currentAngle
Part011Pic012.JPG
Making the Selection Icon Face the Camera 1

Now in the Galaxy script in the MoveSelectionIcon method set the objects rotation to the currentAngle in the CameraController script.

Part011Pic013.JPG
Making the Selection Object Face the Camera 2

Press play. The Selection Icon should now work properly!

That concludes this part of the tutorial. Now we have a good foundation to our galaxy and solar system environments, in the next part we will start to look at spiral galaxies!

scripts_for_part_11 (zip file)

Part 12 Spiral Galaxies

 

1 Comment »

Leave a Reply