Module 2 · In-class exercise 2

Control, Prefab, Camera, Collision and UI

Overview

Work with prefabs, the Camera component on the MainCamera, collision, and UI. You can start this exercise from Module 2 Example 6.

Sprites for this exercise

1

Working with Prefab

15 min
Review GameObject creation
  • Create a Plane object of size 1.6 × 1.9 using this texture.
Review scripts

Create a MoveForward script that:

  • At initialization:
    • rotates (in Z) by a random angle between -180 and +180 degrees
    • computes a random speed between 0.5 and 1 units/second
  • Each update: moves the Plane forward at the computed speed.
Practice prefabs
  • Define a prefab based on the forward-moving Plane.
  • Create four instances of the Plane and run to see them flying randomly around.
Learn
  • Working with the Random function
  • Setting rotation with Quaternion.AngleAxis()
  • You can change transform.up to rotate objects
2

The Camera component on the MainCamera

10 min
  • Update the MoveForward script to remove a forward-moving plane when its center is outside the camera bounds.
ChallengeThis one is slightly challenging. If you can't figure it out, look at my solution: the link to my project is at the end of this page, and you can look at my MoveForward.cs script.
Hint: you know the camera position and the height of the world!!
Camera.mainthe MainCamera's Camera component
Camera.gameObject.transform.localPositionlocation of the camera
Camera.orthographicSizecamera half-height
Camera.orthographicSize * Camera.aspectcamera half-width
  • Now run, and see planes flying and disappearing when they leave the camera bounds.
Learn
  • Working with the Camera component
  • Trust the arithmetic you know, and apply it
3

Support collision

15 min
Define collision
  • Create a GreenUp sprite object using this texture (1.5 × 1.8).
  • GreenUp: add a Collider (Is Trigger set to true) and a Rigidbody2D.
  • Plane prefab: add a Collider.
    HintYou're probably modifying an instance of the Plane. You'll have to re-create the prefab, or modify the prefab directly.
Respond to collision
  • In GreenUp, define OnTriggerEnter2D() to destroy the object it has collided with.
LearnNote the difference between editing an instance and editing the prefab.
4

Button

may be many minutes
  • Define a Button (Button - TextMeshPro) that always appears at the bottom-left of the game window, even after the game window is resized.
  • The button should show “Create One”.
    Hint You'll have to “Import TMP Essentials”; there's no need to import the TMP Examples & Extras. The button's label is in a child object of the Button (click the triangle beside the Button).
  • When this button is pressed, create a new Plane prefab at (0, 0).

Solution

For reference, here's what my project looks like at the end of the above exercise.

+

More steps (on your own)

  1. Bring in the Egg from Module 2 Example 2: the Egg prefab (in Resources/Prefabs), EggBehavior.cs, and the Space-bar spawning code in its GreenArrowBehavior::Update(). Example 2's EggBehavior is self-contained, so it works on its own here.
    HintThis world is only 10 units tall (camera Size 5), so lower kEggSpeed in EggBehavior (e.g., from 40 to 5), or the Eggs will leave the screen almost instantly.
  2. Add support for the Egg prefab to be an actively colliding object.
    HintAdd Collider and Rigidbody2D components, and remember to check Is Trigger on the Collider.
  3. In the EggBehavior script, remove the colliding object.
  4. Now try running… Notice the space bar now destroys the GreenUp!
    HintGreenUp is the first object an Egg collides with!
  5. Now disable GreenUp collision (remove the Collider and the Rigidbody2D).
    Alternate solutionIn EggBehavior::OnTriggerEnter2D, check the name of the colliding object and don't destroy it if the name is “GreenUp”. If GreenUp can collide, you can keep eliminating Planes with GreenUp.
  6. Try running again… your Egg will destroy a plane, and keep traveling! How do you fix this?
    HintThe Egg should destroy itself upon collision.
  7. Integrate the GreenUp rotation controls from Module 2 Example 1, and run to make sure you can navigate the GreenUp.
  8. Support rotating the Eggs when spawning them from the GreenUp.
  9. Now you can hit and remove the planes with Eggs spawned from the GreenUp!