Coursework 5
Coursework 5
Coursework 5
In this coursework, you’ll use what you have learned so far to build your second Unity game.
In this lesson, you’ll be making a game called Chaos Ball. It is a fast-paced arcade-style game.
You’ll start by covering the basic design elements of the game. Then, you’ll build an arena
and game objects. Each object type will be unique and given special collision properties.
Then you’ll make the game interactive and playable. You’ll finish this coursework by playing
the game and making adjustments to improve the experience.
The Requirements
This is not a graphically intense game; rather, it relies on scripting and interaction for its
entertainment value. The requirements for Chaos Ball are as follows:
A walled arena to play the game in.
Textures for the arena and game objects. These are provided in the Unity standard assets.
Several coloured balls and chaos balls.
These will be generated in Unity.
A character controller. This is provided by the Unity standard assets.
A game manager. This will be created in Unity.
A bouncy physics material. This will be created in Unity.
Coloured corner indicators. These will be generated in Unity.
Interactive scripts.
DESIGNING THE TERRAIN
Link to Project Assets:
THE ARENA:
The first thing you want to create is an area where the action will take place. The term arena
is used here to give the idea that this level is quite small and walled in. Neither the player
nor any balls should be able to leave the arena. Otherwise, the arena is quite simple, as
shown in the figure below:
TEXTURING
Right about now, the arena is looking pretty pitiful and bland. Everything is white, and there
is only a single wall.
The next step is to add some textures to liven the place up. You need to texture two objects
specifically: the wall and the ground. Feel free to experiment with the texturing as you
complete this step.
You can make it more interesting if you’d like but begin by following these steps:
1. Create a new folder called Materials under Assets in the Project view. Add a material to
the folder (by right-clicking the folder and selecting Create > Material). Name the material
Wall. 2. Apply the Sand Albedo texture to the Wall material in the Inspector view.
You can do this by dragging the material onto the Albedo property or by clicking the circle
selector next to the word Albedo in the Inspector (see the figure below).
3. Drag the Smoothness slider to a value of 0. 4. Set the X axis tiling to 10. 5. Click and drag
the wall material onto the wall object in the Scene view.
Next, you need to make the ground more interesting. Unity comes with some great water
shaders, and you can use them again here:
1. In the Project view, navigate to the folder Standard Assets\Environment\ Water\Water4\
Prefabs. Drag the Water4Advanced asset into the scene.
2. Position the water centrally, at (0, .5, 0).
At this point, you could place the SuperBouncy physics material directly onto the collider of
your wall. The problem, however, is that you will need to add this material onto all the
walls, all the balls, and the player. Basically, everything that collides in this game needs this
material. You can therefore apply the SuperBouncy material as the default material for all
colliders by using the Physics Settings menu. Follow these steps:
1. Select Edit > Project Settings > Physics. The Physics Manager menu opens in the Inspector
view (see the figure below).
2. Apply the SuperBouncy material to the Default Material property.
This menu is also where you can modify the fundamentals of how physics (collision, gravity,
and so on) behaves. For now, though, you should resist the urge to be all-powerful and
leave reality as it is.
GAME ENTITIES
In this section, you’ll create the various game objects required for playing the game. Just as
with the arena wall, it is easier to create one instance of each entity and then duplicate it.
THE PLAYER
The player in this game will be a modified First-Person character controller. When you
created this project, you should have imported the character controller’s package. You will
also want to raise the controller’s camera up so that the player has a better field of vision
while playing. Follow these steps:
1. Drag an FPSController character controller into the scene, from the folder Assets\
Characters\FirstPersonCharacter\Prefabs.
2. Position the controller at (0, 1, 0).
3. Expand the FPSController object in the Hierarchy view and locate the
FirstPersonCharacter child object (which has a camera on it).
4. Position the FirstPersonCharacter at (0, 5, –3.5) with a rotation of (43, 0, 0).
The camera should now be above, behind, and slightly looking down on the controller. The
next thing to do is to add a bumper to the controller.
The bumper will be the flat surface off which the player will bounce balls.
To do this, follow these steps:
1. Add a cube to the scene and rename the cube Bumper. Scale the bumper to (3.5, 3, 1).
2. In the Hierarchy view, click and drag the bumper onto the FPSController object to nest the
bumper onto the controller.
3. Change the position of the bumper to (0, 0, 1) with a rotation of (0, 0, 0). The bumper is
now slightly in front of the controller.
4. Give the bumper colour by creating a new material (not a physics material) called
BumperColor. Set the albedo colour to something of your choosing and drag the material
onto the bumper.
The last thing to do is to tweak the FPSController’s default settings to make it more suitable
for this game. Carefully set everything as per the figure below, noting that the settings that
differ from the defaults are bold.
The ball is now complete, but it still doesn’t do anything. You need to create a script to
move the ball all around the arena. In this case, create a script called VelocityScript and
attach it to the chaos ball. Move the script into a Scripts folder. Add the code snippet below
into the VelocityScript:
using UnityEngine;
public class VelocityScript : MonoBehaviour
{
public float startSpeed = 50f;
void Start ()
{
Rigidbody rigidBody = GetComponent ();
rigidBody.velocity = new Vector3 (startSpeed, 0, startSpeed);
}
}
Run your scene and watch the ball begin to fly around the arena. At this point, the chaos ball
is finished.
In the Hierarchy view, duplicate the chaos ball four times.
Scatter each ball around the arena (be sure to only change the x and z positions) and give
each of them a random y-axis rotation.
Remember that movement along the y-axis is locked, so make sure that each ball stays at a
y position of 2.
Finally, create an empty GameObject called Chaos Balls, position it at (0, 0, 0), and child the
balls to it to keep your Hierarchy tidy.
The Coloured Balls
Whereas the chaos balls are actually a colour (yellow), they are not considered coloured
balls in this game; rather, the coloured balls are the four specific balls needed to win the
game. They should be red, orange, blue, and green. As with the chaos balls, you can make a
single ball and then duplicate it to make the creation of the coloured balls easier. To create
the first ball, follow these steps:
1. Add a sphere to the scene and rename it Blue Ball. Position the sphere somewhere near
the middle of the arena and make sure the y position is 2.
2. Create a new material called BlueBall and set its colour to blue, the same way you set the
colour of the chaos balls to yellow. While you’re at it, go ahead and create RedBall,
GreenBall, and OrangeBall materials and set each one to the appropriate colour. Click and
drag the BlueBall material onto the sphere.
3. Add a rigidbody to the sphere. Uncheck Use Gravity, set the collision detection to
Continuous Dynamic, and freeze the y position under Constraints.
4. Previously, you created the Blue tag. Now change the sphere’s tag to Blue just as you set
the tag for the chaos ball (refer to the figure prior to this section).
5. Attach the velocity script to the sphere. In the Inspector, locate the Velocity Script (Script)
component and change the Start Speed property to 25 (see the figure below). This causes
the sphere to initially move more slowly than the chaos balls.
If you run the scene now, you should see the blue ball moving rapidly around the arena.
Now you need to create the other three balls. Each one will be a duplicate of the blue ball.
To create the other balls, follow these steps:
1. Duplicate the Blue Ball object. Rename the new ball to its colour: Red Ball, Orange Ball, or
Green Ball.
2. Give the new ball a tag corresponding to its name. It is important for the name and the
tag to be the same.
3. Drag the appropriate colour material onto the new ball. It is important for the ball to be
the same colour as its name.
4. Give the ball a random location and rotation in the arena but ensure that its y position is
2. At this point, the game entities are complete. If you run the scene, you see all the balls
bouncing around the arena.
Next, you need to create a script called GoalScript and attach it to the blue goal. Add the
code snippet below to the script:
using UnityEngine;
public class GoalScript : MonoBehaviour
{
public bool isSolved = false;
void OnTriggerEnter (Collider collider)
{
GameObject collidedWith = collider.gameObject;
if (collidedWith.tag == gameObject.tag)
{
isSolved = true; GetComponent().enabled = false;
Destroy (collidedWith);
}
}
}
As you can see in the script, the OnTriggerEnter() method checks the tag of every
object that contacts it against its own tag. If they match, the object is destroyed, the
goal is flagged as solved, and that goal’s light is disabled.
When the script is complete and attached to the goal, it is time to duplicate it. To
create the other goals, follow these steps:
1. Duplicate the Blue Goal. Name the new goal according to its colour: Red Goal,
Green Goal, or Orange Goal.
2. Change the tag of the goal to its corresponding colour.
3. Change the colour of the point light to the goal’s corresponding colour.
4. Position the goal. The colours can go in any corner as long as each goal gets its
own corner. The three other corner positions are (22, 2, –22), (22, 2, 22), and (– 22,
2, 22).
5. Organize the goals under a new empty game object called Goals. All the goals
should now be set up and operational.
The Game Manager
The last element needed to finish the game is the game manager. This controller will
be responsible for checking each goal every frame and determining when all four
goals are solved. For this particular game, the game manager is very simple. To
create the game manager, follow these steps:
1. Add an empty game object to the scene. Move it someplace out of the way.
Rename it Game Manager.
2. Create a script called GameManager and add the code from the code snippet to it.
Attach the script to the game manager.
3. With the game manager selected, click and drag each goal to its corresponding
property on the Game Manager (Script) component (see the figure below).
Code snippet for GameManager script:
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GoalScript blue, green, red, orange;
private bool isGameOver = true;
void Update ()
{
// If all four goals are solved then the game is over
isGameOver = blue.isSolved && green.isSolved && red.isSolved &&
orange.isSolved;
}
void OnGUI()
{
if(isGameOver)
{
Rect rect = new Rect (Screen.width / 2 - 100, Screen.height / 2 -
50, 200, 75);
GUI.Box (rect, "Game Over");
Rect rect2 = new Rect (Screen.width / 2 - 30, Screen.height / 2 -
25, 60, 50);
GUI.Label (rect2, "Good Job!");
}
}
}
As you can see in the script shown in the code snippet above, the game manager has
a reference to each of the four goals. In every frame, the manager checks to see if all
the goals are solved. If they are, the manager sets the variable isGameOver to true
and displays the “game over” message on the screen. Congratulations. Chaos Ball is
now complete!