This week, despite a small group, we finished off the Pin Bowling game. After Christmas we’ll move onto a different game – most likely something 1st person in design.
UI Elements – Text and Buttons
We added two UI Text elements to our game and aligned them to the top left and top right hand corners respectively. One is used for showing the current score and one is used for showing the number of shots remaining. Note that when we add UI elements, Unity automatically adds a Canvas and an EventSystem for us.
UI elements don’t have a standard Transform component, instead they have a RectTransform component which is used to describe their position within their Canvas. To align to a particular location we click on the square below the words “Rect Transform” and select how we’d like to align within the Canvas by clicking one of the options while holding the Shift and Alt keys to set both the pivot and position.
Changes to the Aimer
We updated the aimer so that it now responds to the Up/Down keys to control the strength of our shot. The forward pointing part of the aimer sizes to reflect the currently selected shot strength.
Formerly we were using Input.GetAxis(“Fire1”) in the AimerController’s Update() method to check for the fire key being pressed. One of effect of this was that force was applied to the sphere continuously as long as the key was being pressed. This wasn’t the design we wanted. We want the user to only have a limited number of shots and for the sphere to be pushed once only per key press. To accomplish this we switched to checking Input.GetKeyDown(KeyCode.Space) instead. This method returns true only in the first frame that the key is pressed.
We added a GameManager component attached to an empty object also called GameManager. This component is responsible for:
- Tracking score.
- Knowing how many shots are allowed.
- Tracking how many shots have been taken.
- Showing and hiding the restart button when required.
- Telling the other parts of the program to reset themselves
The completed project can be downloaded from here.