Module 3: UI, Interaction, Game Manager, Gradual Changes, Autonomous Behavior¶
Coverage¶
Unity-specific skills you will learn, practice, and demonstrate include:
- User Interface
- Integrating the editor between the
GameObject
world and the UI coordinate system. - Working with
UI.RectTransform
.
- Integrating the editor between the
- Packages
- Creating and importing Unity packages.
Concepts you will explore and understand include:
- The
lerp
function for gradual rotation and chasing. - Simple implementations of finite state machines (FSMs).
- Randomness and its simple applications in games.
1. User Interface and Packages¶
Example based on Module-2 Example-4:
Run Behavior¶
- Press the spacebar to launch an egg at the slider-bar-value interval.
Package Import¶
- Go to
Assets → Import Package
, and selectSliderWithEcho.unitypackage
. - From the GitHub repository, look under
ClassExample/Packages
. - Locate and import:
- PreFab:
UI-SliderWithEcho
. - Scripts/UI:
SliderWithEcho.cs
.
- PreFab:
- To create your own package:
- Use
Assets → Export
and select the appropriate components, ensuring to include necessary scripts.
- Use
UI: Using the Imported Package¶
- Ensure your scene includes a Canvas (
UI → Canvas
). - Drag the
SliderWithEcho
prefab into the Canvas.- Align the anchor to the lower-left.
- Rename the prefab to
SpawnRate
.
- Configure the
SpawnRate
in the Inspector:mLabelText
: Set this text field.SliderValue
: Customize its appearance (e.g., color).SliderBar
: AdjustMin Value
,Max Value
, andInit Value
.
Note¶
Avoid manually adjusting the positions of SliderWithEcho
’s child objects.
GreenArrowBehavior: Reference to SliderWithEcho
¶
- Drag the
SliderWithEcho
(SpawnRate
) to theGreenArrowBehavior
object in the editor. - The
value()
ofSpawnRate
determines the interval between spawning new eggs.
Additional Concepts¶
- Time.time: The elapsed time in seconds since the game started.
- Package Operations: Learn about importing/exporting prefabs and managing dependencies.
Alternate Implementation¶
- Use
SliderBar
’s callback (event handler) to handle value changes:- Pros: Avoid polling in
Update()
. - Cons: Implicit event references can make debugging difficult.
- Pros: Avoid polling in
Exercise¶
- Implement this alternative approach and explore UI overflow issues, particularly with long text values.
2. Cool Down Bar¶
A continuation of the previous example:
Run Behavior¶
- Press the spacebar to launch eggs while observing the cool-down bar.
Package Import¶
- Import
CoolDownBar.unitypackage
. - Locate and use:
- PreFab:
UI-CoolDownBar
. - Scripts/UI:
CoolDownBar.cs
.
- PreFab:
GreenArrowBehavior: Reference¶
- Link both
SliderWithEcho
andCoolDownBar
to the respective UI instance variables in the editor.
Key Takeaway¶
The CoolDownBar
encapsulates spawn rate logic, illustrating the importance of object-oriented design.
4. Gradual Rotation and Chasing¶
Run Behavior¶
- WASD: Move the egg.
- UpArrow: Chase the egg.
Slider Behavior¶
0
: No rotation.1
: Always points at the egg.- Between
0
and1
: Gradual rotation.
Learned Concepts¶
- Gradual rotation with
Vector3.LerpUnclamped
. - Reusable
PointAtPosition()
for rotation alignment.
6. Autonomous Movement with Randomness¶
Run Behavior¶
- GreenArrow chases the egg. Upon reaching it, the egg spawns in a random position.
Scene Setting¶
- Main Camera includes
CameraSupport
. GameManager
initializes necessary components and defines logic for randomness and boundaries.
8. Finite State Machine¶
Run Behavior¶
- Touch the plane to trigger a series of states (e.g., enlarge, rotate, shrink).
FSM Implementation¶
- Use
enum
for state definitions and transitions. - Case statements and service functions handle each state.
10. FSM + Randomness¶
Run Behavior¶
- Planes cycle through states with randomized durations.
Learned Concepts¶
- Integrating randomness into FSM for natural behaviors.
- Abstracting logic for easier expansion.
7. Orbiting¶
Run Behavior¶
- Eggs orbit around the hero.
- Modify properties (e.g., orbit radius and speed) from the editor.
Learned Concepts¶
- Implementing orbit logic efficiently with minimal code.
- Understanding quaternion operations for advanced movement.