Coursework 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Course Work 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 Concept of the Game


This is a game slightly akin to Pinball or Breakout. The player will be in an arena. Each of the
four corners of the arena will have a colour, and four balls with corresponding colours will
be floating around. Amongst the four coloured balls, there will be several yellow balls, called
chaos balls. Chaos balls exist solely to get in the player’s way and make the game
challenging. They are smaller than the four coloured balls, but they also move faster. Players
will have a flat surface with which they will attempt to knock the coloured balls into the
correct corners.
The Rules of the Game
The rules for this game state how to play and allude to some of the properties of the
objects. The rules for Chaos Ball are as follows:
The player wins when all four balls are in the correct corners. There is no loss condition.
Hitting the correct corner causes a ball to disappear and the corner light to go out.
All objects in the game are super-bouncy and lose no energy on impact.
No ball (or player) can leave the arena.

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:

CREATING THE ARENA


1. Create a new project called Chaos Ball.
2. Click Assets > Import Package and select the Characters and Environment packages.
3. Add a plane to the scene (by selecting GameObject > 3D > Plane). Position the plane at (0,
0, 0) and give it a scale of (5, 1, 5).
4. Delete the Main Camera.
5. Add a cube to the scene. Place the cube at (–25, 1.5, 0) and scale it to (1.5, 3, 51). Notice
how it becomes a side wall for the arena. Rename the cube Wall 1.
6. Save the scene as Main in a Scenes folder.

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).

CREATING A SUPER-BOUNCY MATERIAL


You want objects to bounce off the walls without losing any energy, and what you need for
this is a super-bouncy material. If you recall, Unity has a set of physics materials available.
The bouncy material provided, however, is not quite bouncy enough for your needs.
Therefore, you need to create a new material, as follows:
1. Right-click the Materials folder and select Create > Physic Material. Name the new
material SuperBouncy.
2. Set the properties for the super-bouncy material as shown in the figure below. Basically,
you want to ensure that the balls are 100% bouncy, so they keep moving at the same speed.

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.

FINISHING THE ARENA


Now that the wall and ground are complete, you can finish the arena. The hard work has
been done, and all you need to do at this point is duplicate the walls (by right-clicking in the
Hierarchy view and selecting Duplicate). The exact steps are as follows:
1. Duplicate the wall once. Place the new instance at (25, 1.5, 0).
2. Duplicate the wall again. Place it at (0, 1.5, 25) with a rotation of (0, 90, 0).
3. Duplicate the wall created in step 2 (the one that’s turned) and place it at (0, 1.5, –25).
4. Create an empty game object called Walls. Set the position of the new object to (0, 0, 0).
Group the four walls you created under this new placeholder object.
Your arena should now have four walls without any gaps or seams.

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 Chaos Balls


The chaos balls will be the fast and wild balls flying around the arena and disrupting the
player. In many ways, they are similar to the coloured balls, so you will be working to give
them universally applicable assets. To create the first chaos ball, follow these steps:
1. Add a sphere to the scene. Rename the sphere Chaos and position it at (12, 2, 12) with a
scale of (.5, .5, .5).
2. Create a new material (not a physics material) for the chaos ball called ChaosBall and set
the albedo colour to a bright yellow colour. Click and drag the material onto the sphere.
3. Add a rigidbody to the sphere. As shown in the figure below, uncheck Use Gravity. Change
the Collision Detection property to Continuous Dynamic. Under the Constraints property,
freeze the y position because you don’t want the balls to be able to go up or down
4. Open the Tag Manager (by selecting Edit > Project Settings > Tags & Layers), expand the
Tags section by clicking the arrow next to Tags, and add the tag Chaos. While you’re here, go
ahead and add the tags Green, Orange, Red, and Blue as well, as they are all used later.
5. Select the Chaos sphere and change its tag to Chaos in the Inspector view (see the figure
below).

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.

The Control Objects


Now that you have all the pieces in place, it is time to gamify them. That is, it is time to turn
them into a playable game. To do that, you need to create the four corner goals, the goal
scripts, and the game controller. When you are done, you will have a game.
The Goals
Each of the four corners has a specific coloured goal that corresponds with a coloured ball.
The idea is that when a ball enters a goal, the goal will check the ball’s tag. If the tag
matches the colour of the goal, there is a match. When a match is found, the ball is
destroyed and the goal is set to solved. As with the ball objects earlier, you can configure a
single goal and then duplicate it to match your needs. To set up the initial goal, follow these
steps:
1. Create an empty game object (by selecting GameObject > Create Empty). Rename the
game object Blue Goal and assign the tag Blue to it. Position the game object at (–22, 2, –
22).
2. Attach a box collider to the goal and check the Is Trigger property. Change the size of the
box collider to (3, 2, 3).
3. Attach a light to the goal (by selecting Component > Rendering > Light). Make it a point
light and make it the same colour as the goal (see the figure below). Change the Intensity of
the light to 3 and the Indirect Multiplier to 0.

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!

IMPROVING THE GAME


Even though Chaos Ball is a complete game, it is hardly as good as it could be.
Several features that would greatly improve gameplay have been omitted. They
were left out for brevity and to give you an opportunity to experiment with the game
and make it better. In a way, you could say that Chaos Ball is now a complete
prototype. It is a playable example of the game, but it lacks polish. You are
encouraged to go back through this hour and look for ways to make the game better.
Think about the following as you play it:
 Is the game too easy or hard?
 What would make it easier or harder?
 What would give it a “wow” factor?
 What parts of the game are fun?
 What parts of the game are tedious?
The exercise at the end of this hour gives you an opportunity to improve the game
and add some features that will improve it. Note that if you get any errors, it means
you missed a step. Be sure to go back through and double-check everything to
resolve any errors that arise.
EXERCISE
One of the best parts of making games is that you get to make them the way you
want. Following a guide can be a good learning experience, but you don’t get the
satisfaction of making a custom game. In this exercise, you have an opportunity to
modify the game a little to make something more unique. Exactly how you change
the game is up to you. Here are some suggestions:
 Add a button that allows the player to play again whenever the game is
completed. (Graphical user interface elements haven’t been covered yet, but
this feature exists in the first game you created, so see if you can figure it
out.)
 Add a timer so that the player knows how long it took to win. Try adding
variations of the chaos balls.
 Add a chaos goal that you must bounce all the chaos balls into. Try changing
the size or shape of the player’s bumper.
 Make a complex bumper out of many shapes.
 Cover up the water with a terrain, plane, or other game objects around the
border of the arena.

You might also like