Module 2 Examples:  Link to GitHub

Coverage

Unity specific skills you will need, practice, and demonstrate include:

·         Working with Unity Prefab

o   Create a prefab in the editor

o   Create/Delete objects at run time

·         Continue working with Transform of GameObject

o   Set positions

o   Set object rotation/orientation

·         User Interface

o   Getting world position from the mouse

·         Object collisions and responses

 

Concepts you will explore and understand include

·         The camera and visible world bound

·         Frame update time vs wall-clock real time

·         Randomness and simple applications in games

·         Direct object control: driving an object

 

1.      Camera bounds

 

2.      Object navigation: All in GreenArrowBehavior.cs

o    Run Behavior: green arrow follow mouse, or <Space> and navigate with WASD

§  Space bar: to toggle mouse positioning vs keyboard WASD

o    Public variables: show up in Inspector and you can change these during runtime via the editor!

§  WATCH OUT: changes made during runtime in the editor WILL NOT be saved!

§  Mouse vs Keyboard control can be toggled

§  Travel speed/rotation speed can be controlled

o    Speed: Distance per unit time. Update() function is called at about 60 times per second!

§  mHeroSpeed: is units per second (Unity defines: Time.smoothDeltaTime) to convert frame update time period to wall clock time

§  Remember total height of the world is 200 units. Verify travel from top to bottom is about 10 seconds

§  mHeroRotateSpeed: 45-degree/second, takes about 8 seconds to do a 360-degree rotation

o    Mouse position: must translate pixel position to camera space

§  Make sure to zero out the z-component

o    GetKey vs GetKeyDown:

§  State of a key vs. Event of a key went down

§  Many times vs only once!

o    Navigation:

§  Move towards the original y-direction: this is transform.up

newPosition  = oldPosition + (Speed * Time.smoothDeltaTime * transform.up)

§  Rotate left/right: rotation with respect to the z-axis or the transform.forward

Transform.Rotate(transform.forward, RotateSpeed * Time.smoothDeltaTime)

o    Learned:

§  Public variables show up in the Inspector window: Convenient!

§  WATCH OUT: The configuration of these variables in the editor when the game is running will NOT be saved!

§  Frame rate vs real time wall clock

§  Unity variable: Time.smoothDeltaTime

§  Access mouse position (via the camera!)

§  Navigate an object by keyboard

§  How to move forward: transform.up/forward/right variables

1.      Note: these directions updates as you change the objects orientation

§  How to rotate: transform.rotate function changes the transform.up (forward/right) directions!

 

3.      Pre fab: Editor create prefabs and instantiate at runtime

4.      Game Manager + Simple UI Text

o    Script Icon: What is going on? Why does this look different? Danger signal! Potential name clash!!

o    Run Behavior: space-bar to spawn eggs, see the echo count on screen

o    Create UI:

§  RMB: UI => Text (Canvas and EventSystems created automatically if not already there)

§  UI elements MUST BE child of Canvas, otherwise, will not show

§  Zoom out to see two spaces: Camera and UI

§  UI elements are in pixel space! (NOT camera space)

§  Size of UI space corresponds 1:1 to Game Window

§  Use Layer manager show/hide UI

 

o    GreenArrowBehavior: includes an int to keep track of number of eggs on screen

o    EggBehavior:

§  Static variable: refer to GreenArrowBehavior

§  What is a “static” variable?

§  Tells GreenArrowBehavior when an egg is destroyed

o    GameManager: central object to coordinate the entire game state

§  Hang off the MainCamera (always there), can also hang off an empty object

§  Two public variables must be set in the editor before game starts

§  GreeArrowBehavior and

§  UI-Text for echoing egg status (UI-CanvasàEggStatusEcho [a UI Text object])

§  Singleton pattern: One instance in the world, “well-known”

§  What is a “static” variable?

§  This is a GLOBAL variable!

§  Note: Play the important role of “central coordinator”

§  Connect GreenArrowBehavior to EggBehavior

§  Central place for echoing state of the game

o    Learned:

§  GameManager: Need to a central coordinator, often enforce to only have one

§  static declaration in a class

§  Class Variable persist for the entire process life time

§  Will persist over different levels (scenes)

§  UI: Two objects created automatically:

§  A Canvas object will be created, all UI objects must be a child of this canvas

1.      All UI elements must be a child to this object! (otherwise will not be visible)

§  An EventSystem object, I drag the EventSystem to be a child of the Canvas (for organization purpose).

§  Default UI-Layer: to see or not to see

 

5.      Collision + Color + Texture: this WebGL link is useless as object manipulation must be done in the editor window!