Lab 3 - Roll A Ball: Game Mechanics
This assignment is due on Friday 4/20.
Continue off of Lab 2 for this assignment.
We will be following these video tutorials:
The written description for this lab should describe most of the steps, but the videos go over a lot of tips and tricks and minor editor features. I recommend watching the videos to do this lab!
Step 1: Create Collectible Objects
- Create a cube and name it Pickup
- Reduce the scale in all dimensions to 0.5
- Set the rotation in all axis to 45 degrees
- Raise it up 0.5 units on the Y axis
- Make the Pickup object animate with a rotation that changes every frame
- Add a script component to the Pickup
- In the Update() method, call the
Rotate
method on the object’stransform
to rotate the object every frame - Use a
new Vector3()
with different amounts for X, Y, and Z that are somewhere around 10 to 50. - Multiply (scale) this Vector3 by
Time.deltaTime
to make it so the animation occurs smoothly based on the framerate - E.g.:
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
-
Test to make sure the cube is spinning
- We want multiple pickups, so make a Prefab from this Pickup game object.
- Make a
Prefabs
folder in the Project Window. - Drag the
Pickup
game object into this folder - this action (dragging a game object into the project assets location) creates a Prefab - Create an empty game object called
Pickups
- Make sure the parent object
Pickups
is at the origin (in the Inspector, click the gear to the right of Transform and select Reset) - Drag our first pickup object onto this parent object.
- Copy and paste the
Pickup
object to create 12 objects and place them around the game space.
- Make a
- Make the pickups a different color so they stand out.
- Make a new material and change it’s albedo to a bright color, like yellow.
- Apply the material to one of the Pickup objects.
- With that object selected, find “Prefab” in the Inspector View and click “Apply” - this applies the Prefab change to all objects with that Prefab.
-
All of your pickups should change color.
- Alternatively, you can apply your material directly to the prefab (in the Project Window) to immediately affect all instances of the prefab.
Step 2: Counting Points
- We want to remove the Pickups when they are hit by the player.
- First make it so that we can detect which objects are Pickups and which aren’t.
- Select the Pickup prefab and in the Inspector Window, click on Tag and select Add Tag…
- Add a tag called “Pickup”
- Go back to the Prefab and apply our new Tag.
- Use collider callbacks to remove Pickups when they are collided
- Open the PlayerController script
- Add a new method
void OnTriggerEnter(Collider other)
- This event callback will be called when the player collider hits another collider
- Check whether the collided object is a Pickup using
other.gameObject.CompareTag ("Pickup")
- If the object is a pickup, hide it using
other.gameObject.SetActive (false);
-
Playtest the game. What happens?
-
The video has a more in-depth explanation of Unity physics, but what we need to do is change the Box Collider of our Prefab to a Trigger.
- Either edit the Prefab directly, or make the change to one of the Pickup objects and apply it to the Prefab.
- Under the Box Collider in the Inspector Window, select Is Trigger
- While we’re at it, let’s optimize the physics of our Pickups
- Add a RigidBody component to the prefab.
- Under the RigidBody in the Inspector, check the Is Kinematic checkbox
- This make’s it so the picks are “dynamic” (Unity expects them to move, and handles them more efficiently) and “kinematic” (so that gravity and other forces do not affect them)
Step 3: Display the Score
- We need to keep track of the score.
- Open the PlayerController script component
- Add a private int field
count
- In the
Start()
method, initializecount
to 0 - In the
OnTriggerEnter()
method, incrementcount
by 1 when a Pickup is picked up
- Add a private int field
- Back in the Unity editor, add a UI → Text element in the Hierarchy Window
- (It will be added underneath an automatic “Canvas” object and an “EventSystem” object)
- Rename the text element “Count Text”
- Change the text color to white in the Inspector Window
- Change the placeholder text to
Count Text
- Click the cross hair under Rect Transform to change the Anchor Preset
- We also want to set the pivor and position, so hold
Shift
andAlt
(might change on different platforms) and select the top left.
- The text should be in the upper left corner of the screen now, but it is wedged way up against the edge
- Change the Pos X and Pos Y values to move the text down and to the right about 10 units
- Let’s update the text in the Player Controller script whenever something is picked up.
- First add
using UnityEngine.UI
so we can work with UI code - Now add a new field to the controller
public Text countText
to reference the text element - Add a function
void SetCountText()
that updates thecountText.text
to"Count: " + count.ToString();
- Call this function whenever
count
changes. - Back in the Editor, Set the
Count Text
property of the Player Controller (Script) to the Count Text object we created, using drag-and-drop
- First add
- Add some text to indicate when the game is won.
- Add another UI → Text element called “Win Text”
- Set the color to white
- Set the alignment to center and middle
- Set the placeholder text to
Win Text
- Change the Pos Y value so the text is not on top of the player
- Back in the Player Controller, add a reference to this new text
- In
SetCountText()
, set the “Win Text” toYou Win!
when all Pickups are collected
- In