Reader small image

You're reading from  Unity Certified Programmer Exam Guide - Second Edition

Product typeBook
Published inMay 2022
Reading LevelIntermediate
PublisherPackt
ISBN-139781803246215
Edition2nd Edition
Languages
Right arrow
Author (1)
Philip Walker
Philip Walker
author image
Philip Walker

Philip Walker originally started as a 3D game artist, but then decided he wanted to combine his current skills with coding so that he could see through the majority of his games' and apps' development himself. Philip has worked in various different industries as an artist and as a Unity developer using various types of technology and techniques.
Read more about Philip Walker

Right arrow

Chapter 7: Creating a Game Loop and Mock Test

In the previous chapter, we moved from the testLevel scene (where we controlled the player ship) to a shop scene (buying and calibrating the player's ship). In this chapter, we will be following a similar trend of stretching out to the rest of the other game scenes in our Scene folder (found in the Project window in the Assets folder).

As part of scene management, all games we play have something called a "Game Loop" – if you're not familiar with the term, it basically means our game will have alternative routes to take. Each route will load a particular scene. We will need to cater for either outcome at each stage of the game.

Eventually, all game loops will loop back to somewhere near the beginning. The following image shows what our game loop will look like by the end of this chapter:

Figure 7.1 – Killer Wave's Game Loop

Referring to the game loop image still, each...

Core exam skills covered in this chapter

The following are the core exam skills that will be covered in this chapter:

Programming core interactions:

  • Implement and configure game object behavior and physics
  • Implement and configure camera views and movement

Working in the art pipeline:

  • Understand materials, textures, and shaders, and write scripts that interact with Unity's rendering API
  • Understand 2D and 3D animation, and write scripts that interact with Unity's animation API

Programming for scene and environment design:

  • Identify methods for implementing Game Object instantiation, destruction, and management
  • Recognize techniques for structuring scripts for modularity, readability, and reusability

Technical requirements

The project content for this chapter can be found at https://github.com/PacktPublishing/Unity-Certified-Programmer-Exam-Guide-Second-Edition/tree/main/Chapter_07.

You can download the entirety of each chapter's project files at https://github.com/PacktPublishing/Unity-Certified-Programmer-Exam-Guide-Second-Edition.

All the content for this chapter is held in this chapter's unitypackage file, including a Complete folder that holds all of the work we'll carry out in this chapter.

Check out the following video to see the Code in Action: https://bit.ly/37Rie3B.

Transitioning our player ship

Currently, our levels can only be completed when the player dies, causing the level to restart, or when the player loses all three lives. Only then are we taken to the game over screen. We now need to start thinking of how a player starts and ends a level. Currently, the player just appears at the start of a level.

In this section, we are going to write some code that animates our player into the scene and, when the level completes, we will have the player ship exit the camera view.

So, let's make a script the same way we did all the other scripts (Chapter 2, Adding and Manipulating Objects, if you need a reference). Name the script PlayerTransition and make sure we have the file in our Script folder in the Unity Editor Project window.

We now need to attach the PlayerTransition script to our player_ship prefab:

  1. Load up the testLevel scene from Assets/Scene in the Project window.
  2. Then, navigate to the Assets/Prefab/Player folder...

Expanding our ScenesManager script 

In this section, we are going to make our ScenesManager script recognize levels 2 and 3 from our scenes folder (Assets/Scene). We will then add these levels to the game loop. Also, we will be adding a game timer for each level. When the timer reaches its limit, we can then trigger the player leaving the level with an animation that will play out. Lastly, we will add a few common methods to move the game onto the next level.

Let's start by opening the ScenesManager script (Assets/Script/Scenesmanager.cs) and adding some variables to assist with what we were talking about. Follow these steps:

  1. At the top of the ScenesManager script, add the following variables:
      float gameTimer = 0;  float[] endLevelTimer = {30,30,45};  int currentSceneNumber = 0;  bool gameEnding = false;

The gameTimer variable timer will be used as our current counter to time how long the level has left until it...

Preparing to loop our game

In this section, we are going to move away from the testLevel scene and introduce three other levels (level1, level2, and level3) to demonstrate the game loop.

By the end of this section, our game loop will be complete. We will be able to start our game from the bootUp scene. From there, we will be able to progress through each scene.

Let's start by removing the placeholder levels in the Unity Editor. Go to the Project window and the Assets/Scene location. Follow these steps:

  1. Make sure your player_ship is saved (Overrides | Apply All) and deleted from the testLevel scene. Next, Delete level1, level2, and level3.
  2. Select testLevel, hold the Left Ctrl (Command on the Mac) key on the keyboard, and press D twice. We should now have three testLevel instances.
  3. Rename testLevel to level1.
  4. Rename testLevel 1 to level2.
  5. Rename testLevel 2 to level3.

We now need to check the Build Settings window to check on the order of our...

Summary

In this chapter, we created a game loop; these are fundamental to game development, and sometimes application development. To create a game loop for our project, we needed multiple scenes that served their own purposes. We also needed to know when a scene started and when it should end. A scene ends when the player presses a button to continue, such as the 7 scene, or when the bootUp title automatically moves onto the next scene after so many seconds.

Apart from making our game loops, we also learned some new vector math components on the way, including Mathf.round, which is used to round off figures Vector3.distance, which is used to measure the distance between two Vector3 points; and Vector3.lerp, which is used to interpolate between two Vector3 points.

These are useful components in game development and will also likely be mentioned in the exam.

In the next chapter, we will be adding some polish to our placeholder scenes with custom fonts, creating our own images...

Mock test

  1. What would be the best way for a UI menu system to be worked on from a programmer's perspective, but at the same time in a way that doesn't interfere with an artist working on the same workflow?
    1. Make it so that each UI component has its own class so that any art changes won't affect either outcome.
    2. Give each UI component a separate material so that any changes in the code will be isolated.
    3. Use prefabs for each UI component so that any artist can modify them individually. 
    4. Have a separate script that sweeps through all UI components to check any changes that are made so that they're known to everyone.
  2. An Image component has a sprite in its Source Image parameter and its Image Type is set to Filled. What does Filled do?
    1. Fills open spaces in the sprite.
    2. It offers various ways to fill in the sprite.
    3. Makes it so no other sprite can override it.
    4. Inverts the color of the sprite.
  3. What component does CrossPlatformInputManager replace?
    1. anyKey
    2. Input...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Certified Programmer Exam Guide - Second Edition
Published in: May 2022Publisher: PacktISBN-13: 9781803246215
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Philip Walker

Philip Walker originally started as a 3D game artist, but then decided he wanted to combine his current skills with coding so that he could see through the majority of his games' and apps' development himself. Philip has worked in various different industries as an artist and as a Unity developer using various types of technology and techniques.
Read more about Philip Walker