Project1 GameObjects

Overview of Project1 GameObjects and C# Scripts

Project1: MiniGame Game Objects

For a simple 2D game, we'll focus on simple 2D game-mechanics and UI-display of game-stats to provide feedback to the player:

GameObjects Descriptions:

  1. Player gameObject that can be controlled by the user to interact with other objects in the game-world.

    • User-Input: Input-Events control movement, animation

    • Interaction: Collision-Events modify: Score, Health, Collected Items, etc. Logic for handling event: OnTriggerEnter2D( )

    • GroundCheck: for Jump, Layer: Ground , Floor

  2. PickUp objects: gameObjects that the Player gameObject interacts with to change the game's data.

    • Interaction: Collider2D, isTrigger, OnTriggerEnter2D( ), Collision-Events - destroy or inactivate gameObject

    • Movement - optional, add Rigidbody2D for simple falling movement (will need a 2nd, nested Collider2D to prevent from falling through the floor)

    • Value, PickupType: PickUp script component provides method to store value, PickUpType type

    • Tags: Collectible, Hazard

    • Prefabs - creates preconfigured gameObject that can be instantiated via code

  3. GameData - Singleton gameObject to store score, health, etc.

ToDo: Add the following GameObjects and Scripts

  1. User Interface - PlayerStats Display - Display changing score, health to give player feedback about game-play actions.

  2. Utility: C# Static Class with Static Methods, ex: modify CanvasGroup alpha

  3. Spawner - Script on Empty GameObject. Script will randomly instantiate PickUp prefabs.

  4. MiniGameManager - Script on Empty GameObject. Script will contain logic to Start, Restart the game and determining when the game is over due to win / lose conditional logic

    1. MiniGameState enums - manage GameState: idle, active, win, lose

    2. StartButton visibility controlled by MiniGameManager logic

      1. StartButton executes Listener Method: RestartGame( ) to set MiniGameState to MiniGameState.active

    3. ResultsPanel - visibility and text - controlled by MiniGameManager logic

      1. ResultsPanel Shows Game Conclusion Text - has CanvasGroup so is hidden when not in use

Sorting Layers is the preferred method for ordering sprite layering for rendering. Unity Tutorial Video on SortingLayers

Project 1 GameObjects, Scripts

Steps to Create and Configure Player GameObjects:

  1. Player gameObject

    • Add Physics2D > RigidBody2D Component - this is required for objects that will have movement, physics forces should be used to give movement to gameObjects.

      • Set Constraints: Freeze Rotation Z-axis

    • Add Physics2D > Collider2D Components - select 1 or more Colliders to fit your gameObject. Select the Edit-collider button to change the size of the collider, manually change the x or y offset. Collider gives physical collision boundary game objects.

    • C# Script: PlayerController - Versions 1-4 - includes logic for movement, collision, update score, health

    • Add PlayerController as a Script Component to Player GameObject

    • Create, and configure Sorting Layer: Player

    • Create a Prefab from the Player gameObect

  1. Pickup: Prefabs Create Several 2D Sprite Game Objects: (objects for the player to interact with - we'll call these PickUp objects )

    • Add one or more Physics2D > Collider2D components to create a boundary for each object

    • Set IsTrigger checkbox for these Collider2D components - will generate execution of onTriggerEnter2D( ) in PlayerController.cs

    • Create C# Script: PickUp

      • Add PickUp as a Script Component to each PickUp gameObject: Set the value, PickupType type in the Inspector.

    • Create Tags: Collectible, Hazard

    • Add Tag: Collectible or Hazard to each PickUp

    • Create, set custom Sorting Layer: Spawned

    • Create a Prefab from each type of PickUp object

  2. Floor - Create an empty GameObject, attach a BoxCollider2D, edit the collider so it is a wide rectangle, move toward the bottom of the screen.

    1. Create, Set custom physics Layer: Ground (for Player jump behavior)

  3. GameData: Create C# Script: GameData

    • Add GameData as a Script Component to new Empty GameObject named: GameManager

New GameObjects, Scripts

  1. PlayerStats UI-Display: Create c# Script: PlayerStats - Add Script to a UI-Panel that has 2 children: UI-Text ScoreText, UI-Text HealthText.

  2. Utility (Static Class) Create C# Script: Utility - Static class, Static methods:This script does not inherit from MonoBehaviour, it will not be put on a gameObject in any scenes. Because Utility is a static class: it's public static methods will be accessible to other scripts using the class name, and then the desired method to be executed: Utility.ShowCG( somePanel );

  3. Spawner Create C# Script: Simple Spawner Add Script to an Empty GameObject: Spawner,

    1. In inspector panel, add PickUp prefabs from assets folder to the Spawner script component for GoodPrefab, BadPrefab attributes.

  4. ResultsPanel - becomes visible to show final MiniGameState: win / lose, otherwise CanvasGroup alpha = 0, to hide panel

  5. StartButton - functions as event-listener - executes ReStartGame(

  6. MiniGameManager Create C# Script: MiniGameManager, Add Script to Empty GameObject: MiniGameManager

    1. In inspector panel, add ResultsPanel, StartButton, Spawner as attributes to the MiniGameManager script component

Last updated