Overview
This lecture provides a step-by-step, practical introduction to game development in Unity by guiding you through creating a Flappy Bird clone, covering essential Unity concepts, basic programming, and UI setup.
Getting Started with Unity
- Download and install Unity Hub, then the Unity Editor (2021.3 used) and Microsoft Visual Studio for coding.
- Create a free Unity account to use the editor.
- Start a new 2D Core project, name it, and create.
Unity User Interface & GameObjects
- Unity UI comprises Project, Hierarchy, Inspector, and Scene/Game View panels.
- Project: stores assets like sprites and scripts.
- Hierarchy: lists all GameObjects in the current scene.
- Inspector: modifies selected GameObject's components.
- Scene/Game View: visualizes and edits the scene.
- GameObjects are containers with position, rotation, and scale; components add functionality.
Making the Bird Move
- Add Sprite Renderer and Rigidbody 2D components to the bird GameObject.
- Add a Circle Collider 2D for collision detection.
- Create a C# script; use public references to access other components (e.g., Rigidbody2D).
- Use Update() for per-frame logic; Start() runs once.
- Detect spacebar press with Input.GetKeyDown(KeyCode.Space) inside an if statement to apply upward velocity.
- Expose flap strength as a public float variable to adjust in Inspector.
Spawning and Managing Pipes
- Build pipes with GameObjects, adding Sprite Renderers and Box Colliders.
- Nest top/bottom pipes under a parent for group movement.
- Make pipes move left by modifying transform.position in Update(), using Time.deltaTime for frame-rate independence.
- Use prefabs to create reusable objects; spawn new pipes via a spawner script at timed intervals.
- Randomize pipe spawn heights with Random.Range.
- Destroy pipes when they move offscreen with Destroy(GameObject).
Tracking Score and UI
- Add a UI Canvas and Text object for displaying score; set canvas to scale with screen size.
- Create a Logic Manager GameObject with a script to track score as an int and update the UI.
- Use triggers (isTrigger checked) for non-physical collision detection (bird passing through pipes).
- Reference scripts via tags (e.g., โLogicโ) and GameObject.FindGameObjectWithTag for runtime connections.
- Pass variables as function parameters for flexibility.
Game Over and Restart Logic
- Implement a Game Over screen UI and a restart button triggering a public function in the Logic script.
- Use UnityEngine.SceneManagement to reload the current scene when restarting.
- Show the Game Over screen only when necessary by enabling/disabling the GameObject.
- Stop gameplay actions after Game Over using a boolean (bool) to track if the bird is alive.
Key Terms & Definitions
- GameObject โ The basic entity in Unity representing objects in the scene.
- Component โ Modular feature that adds functionality to GameObjects.
- Prefab โ A reusable blueprint for GameObjects.
- Rigidbody2D โ Component that gives GameObjects physics properties.
- Collider/Trigger โ Detects collisions; trigger allows overlap detection without physical collision.
- Script (C#) โ Custom code controlling behavior, attached as a component.
- Update() โ Function called every frame.
- Start() โ Function called once at the start.
- Inspector โ Panel to view and edit GameObject components.
- Time.deltaTime โ Ensures smooth, frame-rate-independent movement.
- Input.GetKeyDown() โ Checks for key press in the current frame.
- Tag/Layer โ Identifiers for grouping or filtering GameObjects.
- Bool โ Boolean value; true or false.
Action Items / Next Steps
- Add Game Over for the bird going offscreen.
- Fix score updating after Game Over.
- Add sound effects via an Audio Source and script reference.
- Use Particle System for effects (e.g., clouds).
- Animate the birdโs wings using the Animation window.
- Create a title screen as a separate scene and add it to build settings.
- Implement high score saving with PlayerPrefs and display it on UI.
- Try remaking another simple game (e.g., Pong, Breakout) to reinforce concepts.