Exercise 3 · Team work

Object Behaviors and GUI

Build on Exercise 2: add a cool-down bar, six waypoints you can shoot, and enemies that patrol the waypoints by turning gradually toward them. Due date: see the course website.

Objective

By now you can control the position and rotation of GameObjects and you have a sense of frame time vs. real time. In this exercise you'll practice refined movement with the lerp function, give objects simple autonomous behaviors, and work with both of Unity's coordinate systems: the game world and the UI.

Team workThis is a team exercise. Only one copy needs to be submitted for each team.

Coverage

▭User interface

  • Sharing the screen between the game world and the UI coordinate system
  • Working with a UI RectTransform

↻Lerp

  • Gradual rotation toward a target
  • Chasing a target that can move

✦Behaviors

  • Patrolling a set of waypoints
  • Randomness in games (again)

◆From EX2

  • Prefabs, collisions, status text
  • Rates per second with Time.smoothDeltaTime
Start from the class examples Module 2.5 shows the cool-down bar, and Module 3.1 shows PointAtPosition(), gradual rotation and chasing.

What to build

Play my solution first. There are four kinds of objects: the hero, eggs and enemies from EX2, and a new waypoint system. Start from your own EX2 project, or from my EX2 solution once it's posted. As before, all speeds and rates are per second, so multiply them by Time.smoothDeltaTime.

1

Everything from EX2 still works

The camera (Size 100), hero, eggs, enemy spawning and collisions, status text and Q all behave exactly as in Exercise 2, with these changes:

  • Eggs are also destroyed when they hit a (visible) waypoint.
  • Enemies now move: they patrol the waypoints (below).
  • The hero does not collide with waypoints; it simply passes over them.
2

Cool-down bar

A UI bar shows how much of the 0.2-second egg cool-down is left:

  • Right after an egg is fired, the bar is 200 pixels wide.
  • It shrinks gradually to 0 pixels over the 0.2 seconds, when the next egg is allowed.
  • Place it in the UI (e.g., under the status text).
You may use the class package CoolDownBar.unitypackage from Module 2.5 is fine to use. Set its width to 200 and call SetCoolDownLength(0.2f); then fire only when TriggerCoolDown() returns true. That bar redraws at full width once the cool-down is over to show "ready", which is acceptable.
3

Waypoints

Six waypoint objects, named A to F, each an image sprite at Transform Scale (8, 8, 1) showing its letter (the WayPointLabels images in EX3_Textures.zip), at:

ABCDEF
(-70, 70)(70, -70)(30, 0)(-70, -70)(70, 70)(-30, 0)
EventResponse
Egg hits a waypointThe egg is destroyed and the waypoint's alpha decreases by 0.25.
4th egg hitThe waypoint jumps to a random position within ±15 units in x and y of its initial position (not its current one), and is fully opaque again. So a waypoint never ends up more than 15 units from where it started.
HToggles hiding the waypoints. Hidden waypoints aren't hit by eggs, but enemies still patrol through them.
4

Enemy patrol

  • Every enemy travels along its own transform.up at a constant 20 units/second.
  • At all times an enemy has a target waypoint. A newly spawned enemy starts with a random target.
  • Each frame, it turns gradually toward the target by calling PointAtPosition() from Module 3.1 with a rate of 1.5 per second, i.e., PointAtPosition(target, 1.5f * Time.smoothDeltaTime).
  • When an enemy comes within 25 units of its target, it picks the next waypoint as its new target and starts turning toward it.
  • If the target waypoint is re-positioned (after its 4th hit), the enemy heads for the new position.
Mode (J toggles)Next waypoint
Sequence (default)The next letter: A → B → C → D → E → F → A …
RandomA random waypoint that is different from the current one.
// Module 3.1: gradually turn transform.up toward p, at rate r
// (v is normalized, so the turning speed doesn't depend on distance)
private void PointAtPosition(Vector3 p, float r)
{
    Vector3 v = (p - transform.localPosition).normalized;
    transform.up = Vector3.LerpUnclamped(transform.up, v, r);
}
5

Status text

Everything from EX2, plus the waypoint mode (Sequence/Random). For example:

HERO: Drive(Mouse)  TouchedEnemy(3)
EGG: OnScreen(12)
ENEMY: Count(10)  Destroyed(7)
WAYPOINTS: (Sequence)

Hints

Collisions

Give each waypoint a BoxCollider2D with Is Trigger checked, like the enemies. In the waypoint's OnTriggerEnter2D, respond only to eggs, so the hero passes over. To stop a hidden waypoint from being hit, disable its collider as well as its SpriteRenderer.

Alpha

Same idea as EX2: copy SpriteRenderer.color, subtract 0.25 from the copy's a, and assign it back.

Who picks the next waypoint?

The object that knows all six waypoints and the current mode (for example the Game Manager) can provide a NextWaypoint(current) function that the enemies call. For random mode, pick among the other five.

Why 25 units?

An enemy turns slowly, so it can't hit a waypoint exactly. Switching targets when it gets close makes it curve smoothly on to the next one.

UI and world

The cool-down bar lives in a Canvas, so its RectTransform is in pixels; the hero, eggs, enemies and waypoints are in world units. Switch between the two in the Scene view with the 2D view and by double-clicking the Canvas or Main Camera in the Hierarchy.

Stretch goal (optional, not graded)

  • Show each enemy's current target letter in the status text or above the enemy.
  • Let the player change the turn rate at run time and see how the patrol paths change.

Submission

One submission per team:

  1. A zip file named TeamNameEX3.zip containing your entire project: every file in Assets, Packages and ProjectSettings, and nothing more.
  2. A separate folder with your PC EXE build. We'll double-click the EXE to run your project.

List every team member's name in the submission comment and at the top of your enemy script.

Using an AI assistant? That's fine, but every team member must understand and be able to explain every line you submit. At the top of your enemy script, write a short comment (2–3 sentences, in your own words) explaining how an enemy turns toward its target waypoint and decides when to switch to the next one.

Self-check before you submit

  • I unzipped our zip into a new, empty folder and opened it in Unity 6000.3.11f1. It opens and runs.
  • The zip has only Assets, Packages and ProjectSettings.
  • Double-clicking the EXE runs the game, and Q quits it.
  • All EX2 features still work, including mouse mode and the egg count.
  • The cool-down bar shrinks from 200 pixels to 0 every time an egg is fired.
  • Four egg hits move a waypoint, but never more than 15 units from its starting position.
  • With the waypoints hidden (H), eggs fly through them and enemies still patrol.
  • In sequence mode, enemies visit A, B, C, D, E, F in order; in random mode, never the same waypoint twice in a row.
  • The hero passes over waypoints without affecting them.
  • Our enemy script lists the team members and has the 2–3 sentence explanation comment.

Credit distribution

ItemSub-itemCategory
EX2 features still work: camera, hero, eggs, enemy spawning and collisions, status text, Q15%
Scene setup: six waypoints A–F as image sprites, scale 8, at the given positions5%
Eggs and cool-down bar15%
Destroyed on hitting a visible waypoint5%
Cool-down bar is 200 pixels wide right after firing5%
Cool-down bar width shrinks with the time left, to 0 at 0.2 seconds5%
Waypoints20%
Egg hit: alpha decreases by 0.255%
4th hit: re-positioned within ±15 of the initial position, fully opaque again10%
H hides/shows; hidden waypoints aren't hit5%
Enemy patrol30%
Constant speed of 20 units/second along transform.up5%
Turns gradually toward the target: PointAtPosition(), rate 1.5/second5%
Switches to the next waypoint within 25 units5%
Heads for a target's new position after it's re-positioned5%
Sequence mode: A → F, then back to A5%
Random mode: a different waypoint each time5%
Status text: J toggles, and the text shows, the waypoint mode (Sequence/Random)5%
Proper submission10%
Zip unzips and runs in the editor, with only the four project folders5%
Separate EXE folder; double-click runs the game5%
Total100%
The 15% for EX2 features covers everything in the EX2 credit list; you lose part of it for each EX2 feature that no longer works (for example, if mouse control stops working).