In-Class Exercise 2:
Control, Prefab, Working with the Camera component on the MainCamera,
collision, and UI
You
can start this exercise based on Module 2 Example-6
Step 1 (15 minutes): Working with Prefab
- Review of GameObject creation:
- Review of Script
- Create a MoveForward script to:
- At initialization:
- Rotate (in Z) by a
random degree between -180 and +180 degrees
- Compute a random speed
of between 0.5 and 1 units/sec
- Each Update: Move the
Plane forward at the computed speed
- Practice Prefab
- Define a Prefab based on
the forward moving Plane
- Create four instances of
the Plane and run to see them flying randomly around
- Learn:
- Work with the Random function
- Rotation set by: Quaternion.AngleAxis()
- You can change transform.up to rotate
objects
Step 2 (10 minutes): Work with the Camera component on the MainCamera
- Update the MoveForward script to remove a forward moving plane
when its center is outside the camera bound
- Challenge: this is slightly challenging, if you cannot figure
this out, look at my solution (link to my project is at the end of this
document, you can look at my MoveForward.cs script)
- Hint: You know the camera
position and the height of the world!!
- Camera.main: MainCamera’s Camera componment
- Camera.gameObject.transform.localPosition: location
of the camera
- Camera.orthographicsSize:
camera half-height
- Camera.orthographicsSize * Camera.aspect:
camera half-width
- Now, run to see planes
flying and disappear when are outside of the camera bound
- Learn:
- Work with the Camera component
- Trust the arithmetic you
know and apply
Step 3 (15 minutes): Support Collision
- Define collision
- Create a GreenUp sprite object using this
texture (1.5x1.8)
- GreenUp:
add a Collider (isTrigger set to true) and
RigidBody2D
- Plane Prefab: add a
Collider
- Hint: you are probably
modifying an instance of Plane, you will have to re-create the Prefab,
or, modify the Prefab directly
- In GreenUP,
define onTriggerEnter2D() to destroy the object
that is has collided with
- Learn:
- Note the difference
between editing an instance and the Prefab
Step 4 (may be many minutes): Button
- Define a Button (Button
Text Mesh Pro) that always appear at the top-left of the game window (even
after game window size change):
- The button should show
“Create One”
- Hint: You will have to
“Import TMP”
- No need to import TMP
Examples and Extras
- Label for the button is
in a child object of the Button (click on the Triangle besides the
Button)
- When pressed on this
button, create a new Plane Prefab at (0, 0)
For
reference: this is what my project looks like at
the end of the above exercise.
More Steps (on your own)
- Add in support for the Egg Prefab to be an active colliding
object
- Hint: add Collider and RigidShape
components, and remember to check isTrigger on
the Collider.
- In the EggBehavior script: remove
the colliding object
- Now, try running … your Egg will
- Notice space bar now will destroy the GreenUp!
- Hint: GreenUp is the first
object an Egg will collide with!
- Now, disable GreenUp collision
(remove the Collider and the RigidShape)
- Hint: alternate solution, in Egg::OnCollisionEnter, check the name of the colliding
object and do not destroy if the name is “GreenUp”.
If GreenUp can collide, you can continue to
eliminate Plane with GreenUp.
- Now, try running, … your Egg will destroy a plane,
… AND … continue to travel!
- How to fix this?
- Hint: the Egg should destroy self
upon collision
- Now, Integrate GreenUp rotation controls from Module 2 Example 1
- Run to make sure you can
now navigate the GreenUp
- Support rotating the Eggs
when spawning them from the GreenUp
- Now: you can hit and remove the planes with the Eggs spawned
from the GreenUp!