In this assignment you will create a
brick shooting game using the power cube
and samoflange assets.
You will learn about the rigidbody and
collider components, the instantiate,
destroy and translate functions,
as well as using prefabs.
For more details on these individual elements
see the lessons on them linked below.
You'll need the Robot Lab asset package
for this. See the link on this page
if you don't already have it.
To begin open the empty scene
and then go to File - Save Scene As
to save it as our new scene. Save it in to
the scenes folder of the project and call
it Brick Shooter.
As we've used Save As we are now inside
the Brick Shooter scene. Let's start by
making our wall of cubes.
For this we'll use the power cube.
In the Models folder you will find various
assets. So locate the asset called
Prop_PowerCube.
It looks like this.
Drag and drop it in to the scene
and with your mouse cursor over the scene view
press F to focus on it.
To begin we'll setup the lighting to work
with this. Place this object
on the Dynamic Objects layer
at the top of the inspector
and in the mesh renderer for this object
check the box for Use Light Probes.
Let's position this first brick in the wall
at (-3, 0.5, 0).
Again you can always reselect your object
in the hierarchy and with your mouse cursor
over the scene, press F to refocus.
Now that it's nicely lit we'll add a
collider and rigidbody component to
give it a physical mass.
Using the Add Component button on the inspector
go to Physics and Box Collider
and then Add Component again
and choose Physics - Rigidbody.
We'll be making many of these power cubes
via duplication to form our wall.
But we may want to go back and adjust
properties for all of them, so we should
turn this into a prefab now.
Drag and drop this object into the
Prefabs folder in the project panel.
This saves the object as a prefab
and we'll rename it to differentiate it
from our model. We'll call it
Physics_PowerCube
because it has the two crucial components,
the collider and the rigidbody
which mean that it's controlled by
the physics engine. You should note that
because this is now a prefab it's text
is shown in blue in the hierarchy.
Now let's make the duplicates of this
to form our wall.
Select the object in the hierarchy
and we'll use the hand tool briefly to zoom out.
We can duplicate this by going to
Edit - Duplicate
or by using shortcut Control-D on PC
or Command-D on Mac.
Now we're going to position the bricks
in our wall by using Snapping.
You can use snapping by holding down the
Command key on Mac or Control on PC
and by dragging with the Translate tool
You can see that if I hold down the key
and pull the handle the object only moves
in increments of 1. And because our object
is modelled to a scale of 1 it will snap
in to that position perfectly. We will
then continue to repeat this step
until we have 7 in our first row.
Duplicate
Command drag or Control drag to snap.
Duplicate, snap drag
Duplicate, snap drag and once more.
This is our first row of cubes.
And to organise them nicely we're going to
create an empty game object to
store our row. So go to Game Object - Create Empty
hit the Return key on Mac or F2 on PC
to rename and we'll call this Row.
I'm going to position this in the middle of our row,
so we'll place this at (0, 0.5, 0).
Then to add our power cubes to this row
we will select them,
so select the first one, hold down Shift
and select the last.
And then drag them on to the row in the hierarchy.
Now they are all child objects of Row.
Now we can conveniently duplicate Row
and use snap drag in order to
create the rest of the wall.
So select Row in the hierarchy,
and duplicate.
Snap drag up
Duplicate, snap drag up
and continue this until we have 5 rows.
Now that our wall is complete, let's work
on our projectile.
In the Models folder find the 'prop_samoflange'
asset and drag it in to the scene.
Let's drag it up so that we can see it
and hover and press F to focus.
We'll set up the lighting by putting
it on the Dynamic Objects layer
and checking Use Light Probes
in the mesh renderer.
We need this to be a Physics Object as well
So go to Add Component - Physics - Sphere Collider
and then Add Component - Physics - Rigidbody.
Our projectile is now ready but because
we're going to use a script to create and launch
this we do not want it in the scene when
it begins. So let's save this as a
prefab as well.
Drag it from the hierarchy and drop it in
to the Prefabs folder. Then we'll rename
this Physics_Samoflange as well.
You can now delete this from the scene
by selecting it in the hierarchy
and using Command-Backspace on Mac
or Delete on PC.
Now let's setup our shooting mechanic.
Begin by expanding the Cameras object
in the hierarchy and select Camera 01.
We will reposition this camera at (0, 3, 10).
And set the rotation to (0, 180, 0).
So that now our camera - as you can see
in the game view - is facing our wall of bricks.
Because our camera will move around and shoot
at the wall of bricks we'll place the
code for the shooting onto the camera.
So let's go to Add Component - New Script.
And we'll call this script Shooter.
Double click on the script icon in order
to open it for editing.
We do not need the Start function for this
so we'll begin by removing that
and also remove the comment
above the Update function.
We'll begin with some public variables
so that you can tweak settings or assign
prefabs in the inspector.
First, add a public Rigidbody
variable called Projectile.
We will drag and drop our Physics Samoflange
prefab on to this and use it in our
instantiate function in order to create
instances of our projectile.
Next add a public transform variable
called shotPos.
We will create an empty game object
to store the position to shoot projectiles from
and assign it to this variable later.
Next add a public float
called shotForce
and set it to 1000.
We will use this to define the amount of
force to shoot with.
And finally add a public float called
moveSpeed and set this to 10.
This will define the movement speed of the
camera to aim the shots.
All of our operations must be immediate
so we'll perform them inside the update function.
Start by adding two variables to
represent the horizontal and vertical
input axes.
These are float variables called
H and V.
These are multiplied by Time.deltaTime
for smoothing. And then multiplied by
moveSpeed to increase the speed.
We'll then use these values inside
the translate command in order to move
the camera around. So we'll say
transform.Translate and create a new
Vector3 with the values of H and V
as components of this vector.
So we'll be using the horizontal axis
to move the X axis of the object
and the vertical axis to move the Y axis.
To fire our projectile we'll add
an IF statement that waits for the release
of the fire1 button,
which by default is the left-hand
Control key on the keyboard.
Into this we'll add our instantiate function
to create the projectile instance
and then add a force to it.
To begin, create a Rigidbody variable
called Shot and set it equal to
our instantiate function.
In this function we need the object
to create, the position and the rotation
as it's three arguments -
So we'll use our projectile variable,
our shotPos transform's position
and shotPos's rotation.
We finish the instantiate line by adding
'as Rigidbody' in order to cast the result
of this to the rigidbody data type.
This is because the standard instantiate
function returns an object
and we know that the type of our instantiated
object needs to be a rigidbody.
Finally we use the Rigidbody variable
'Shot' to address that instance of our
projectile stored in it and add a force to it.
We do this in the forward direction of
the shotPos transform,
and multiply it by shotForce.
Now let's save this script and return to Unity.
Reselect the camera and you'll see that our
public variables can be seen
on the script component.
Let's start by assigning the physics samoflange
from the prefabs folder that we made earlier
to the projectile field.
Next we need to add a transform to our scene
to represent the position and direction
with which to shoot.
Create an empty game object
by going to Game Object - Create Empty.
We will rename this shotPos
and then drag and drop it on to camera 1
to parent it beneath it.
Let's reset it's transform so that it
matches that of the camera.
You can see if I zoom out now
that the camera is positioned here
and if I select shotPos,
click on the cog icon and choose
'Reset', then all of the values for the
transform are reset to a default.
And now if I choose the translate tool
you can see that it is at the exact same
position as the camera.
But we need it to be slightly forward
and slightly lower, and also tilted up
in order to aim shots from below camera
in an arc pointed at the wall.
So we'll set the position of shotPos
to 0 in X, -0.5 in Y, and 1 in the Z axis.
As you can see this places it
forward of the camera.
Now we'll tilt it up by putting
-15 in to the X axis.
Now reselect the camera, as this is
where our script component is and drag
and drop shotPos from the hierarchy
and drop it on to the variable shotPos
on the Shooter script component.
And that's it, our script is now ready for use.
Save your scene by going to File - Save Scene
and then press play to test out the game.
Remember that the cursor keys,
or W, A, S and D on the keyboard
are the defaults for horizontal and vertical.
and that the left control key is fire1 - the key to shoot.
One thing we can do is to tidy up our
stray projectiles - as we continue to shoot
we are adding rigidbody objects to the scene
so we should really tidy these up
because otherwise we are adding more and more
for the physics engine to keep track of,
which will eventually effect performance.
Press play to ensure you are no longer
in play mode and then select the physics
samoflange in the prefabs folder.
Scroll to the bottom of the inspector
and click Add Component then choose New Script
and name this script Tidy.
Double click the icon to open it for editing
and for this, all we need is our Start function.
So we can remove the Update function
and we'll also remove the comment.
When this object enters the game we simply
need to use the Destroy command
and we'll destroy the game object this is
attached to - the ball itself.
And because we don't want it to happen
literally when the object enters the game
we will use the second argument of the
Destroy command, which is the timed delay.
And we'll place in 5 seconds.
We'll save our script
and return to Unity
Now you should notice that when you
play test the game, once you have fired
a few projectiles they eventually
remove themselves from the game after they've
existed for 5 seconds. You can
see that by watching the hierarchy
these objects are being removed from the scene
and they disappear from the scene and game views.
So that's our brick shooter complete.
Remember to save your scene
and try experimenting with different objects
to fire, or different rates of fire
or different movement for the camera
and if you didn't understand any of the concepts
in this assignment check out the links
to the lessons below.
Subtitles by the Amara.org community