-
So far we have created a play field
-
and a Player game object
-
and we have the player moving
-
under our control.
-
In this lesson we'll create
-
the collectable objects
-
for our player to pick up.
-
First open the Sprites folder.
-
Drag the sprite Pickup
-
in to the hierarchy.
-
You'll notice after dragging in
-
the Pickup sprite that we
-
still can't see it.
-
That's because it's being rendered
-
behind both the Player
-
and Background sprites.
-
To bring it forward we
-
need to set the sorting layer
-
in the sprite renderer component.
-
Set the sorting layer to Pickups.
-
Now the Player game object
-
is still in the way.
-
This is because the Player
-
sorting layer is in front
-
of the Pickups sorting layer.
-
Let's temporarily deactivate
-
the Player game object.
-
Select the Player game object
-
and deselect the checkbox
-
in front of the Name field.
-
This is the game object's Active checkbox.
-
Deselecting it will deactivate
-
the game object in the scene.
-
This will give us a clear working space
-
for our new pickup object
-
which we can now see.
-
Focus the scene view camera on the
-
pickup object by highlighting it
-
and pressing the F key whilst
-
the cursor is over the scene view.
-
So that we can collide with the pickup
-
let's add a circle collider 2D
-
component to it.
-
With the pickup highlighted click
-
Add Component - Physics 2D
-
then Circle Collider 2D.
-
Adjust the Radius property
-
of the circle collider 2D
-
so that it fits the sprite artwork visually b
-
by dragging in the Radius field.
-
To be effective the collectable
-
must attract the attention of the player.
-
So let's make the object more attractive.
-
There's one thing I feel certainly
-
attracts the attention of a player
-
and that is movement.
-
So let's rotate our pickup.
-
One way to do this is with a script.
-
With the pickup object still selected
-
use the Add Component button
-
in the inspector.
-
Let's create a new script called Rotator.
-
Click Create and Add to confirm
-
and let's organise the script by
-
placing it in the Scripts folder.
-
Open the Scripts folder and
-
double click to open it for editing.
-
We want the sprite to spin and we want
-
to do it with this script.
-
Let's remove the sample code
-
we don't need.
-
We will not be using forces
-
so we can use Update
-
rather than Fixed Update.
-
We want to rotate the object every frame.
-
To make the pickups spin we
-
don't want to set the transform rotation
-
but we want to rotate the transform.
-
Type Transform inside Update.
-
Select it and hold down the
-
control key on windows or the command
-
key on mac and type '
-
Again this brings up the page
-
with a search term Transform.
-
Select Transform.
-
Scroll down until you see Transform's public functions.
-
There are two main ways to affect the transform.
-
These are Translate and Rotate.
-
Translate moves the game object
-
using it's transform.
-
Rotate rotates the game object
-
using it's transform.
-
We will use rotate.
-
So let's click on the link.
-
This brings up the page for Transform.Rotate.
-
Note again the two signatures.
-
One is using a vector3
-
and the other is using 3
-
float values for X, Y or Z.
-
Both have the optional parameter
-
Space which we will
-
leave at default for this lesson.
-
Again we will choose the most simple
-
form that only uses the vector3 for direction.
-
Let's return to our code.
-
After transform,
-
making sure that transform is written to
-
begin with a lowercase t,
-
write .rotate (new Vector3 (0, 0, 45))
-
This means we'll rotate
-
around the Z axis.
-
Now this action also needs
-
to be smooth and frame rate independent
-
so we need to multiply the vector3 value
-
by Time.deltaTime.
-
It is worth noting that even
-
though we are working with a 2D
-
sprite we are using a vector3
-
to rotate the collectable's transform.
-
This is because the transform
-
of the 2D sprite
-
still exists in the 3D volume.
-
When using the transform component
-
with 2D objects we simply
-
ignore the axis we don't need.
-
Now that we are done with our Rotator script
-
save this script and return to Unity.
-
Let's test by entering play mode.
-
And we can see our pickup object rotates.
-
Let's exit play mode.
-
Okay, we have the start of a working
-
pickup object.
-
Next we want to place these pickups
-
around the game area.
-
But before we do this we need to do
-
one important step.
-
We need to make our pickup object
-
in to a prefab.
-
Remember, a prefab is an
-
asset that contains a template
-
or blueprint of a game object
-
or game object family.
-
We create a prefab from an
-
existing game object or game object family
-
and once we create it
-
we can use it in any scene
-
in our current project.
-
With a prefab of our pickup object
-
we will be able to make changes to
-
a single instance in our scene
-
or to the prefab asset itself.
-
And all of the pickup objects in our
-
game will be updated with those changes.
-
You'll notice we already have a folder
-
to hold our prefabs.
-
Let's drag the Pickup game object
-
from our hierarchy and place
-
it in to our Prefabs folder.
-
When we drag and item from our hierarchy
-
in to our project view
-
we create a new prefab
-
asset in our project.
-
Before we spread our collectables
-
around the game area we should create
-
a new game object
-
to hold our pickups and
-
to help organise our hierarchy.
-
Let's create a new game object.
-
And call it Pickups.
-
Check the transform to make sure our
-
Pickups holder object
-
is at origin, or (0, 0, 0).
-
And if not use the component
-
context menu to reset it.
-
Next drag our Pickup game object on to it.
-
Now we want to spread a
-
number of these pickup objects
-
around the play area.
-
Make sure the pickup game object is
-
selected, and not the parent.
-
Now let's back out a little
-
so we can see the entire game area.
-
Click and drag the pickup game object
-
to move it in to the
-
first pickup position.
-
I'm going to place mine at the top of the playing area.
-
With the game object still selected
-
duplicate it.
-
This can be done either by selecting
-
Edit - Duplicate, or by using the hot key combination.
-
This is command-D on mac,
-
or control-D on windows.
-
Now let's position the
-
second instance of the prefab.
-
Using the hot keys we will
-
create a few more, placing them
-
around the play area.
-
Okay, I've created 12.
-
Let's hit play and test.
-
Excellent, these pickup prefabs
-
are working great.
-
In the next assignment we'll learn how to
-
pick them up and to count them.