27
Professor Dr.-Ing. Darren Carlson Department of Electrical Engineering | Academy for Creative Media University of Hawai'i at Mānoa GameObjects and Components Dr. Darren Carlson EE 491F: Multimedia Programming Foundations

Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Embed Size (px)

Citation preview

Page 1: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

GameObjects and Components

Dr. Darren Carlson

EE 491F: Multimedia Programming Foundations

Page 2: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Examples of Rendering Complexity

2https://www.youtube.com/watch?v=ld52Li5amaE

Page 3: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Example Bicycle Instance

Review: Classes and Objects

3

Classes formally define the structure, properties, and behavior of objects.• Classes are used to construct usable

software objects in memory. • An object stores its state in internal

variables and exposes its behavior through methods.

• Multiple objectscan be createdfrom a singleclass

Example Bicycle Class

What other functionality could we add to this class?

b1

b2

New

Page 4: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Review: Unity Scripts are Based on Classes

4

Start() and Update() are both special methods in Unity's version of C#.

Unity Lifecycle Methods

Page 5: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Review: Start() Versus Update()

5

Scripts attached to GameObjects follow a lifecycle, which is defined by various methods (we introduce two below, but there are several)

• Start is called by Unity on the frame when a script is enabled. Start is called exactly once in the lifetime of the script (See A below)

• Update is called every frame, if the Script is enabled (See B below)

A B

Page 6: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Aside: Frames in Film and Video

6

• Historically, films were composed of strips of celluloid containing thousands of individual pictures (known as frames).

• When subtly changing still images are shown in quick succession, it produces the illusion of movement (think of a flipbook).

• Television and computer video uses the same technique .

Example of how frames create the illusion of movement Source MT Aviation

https://www.youtube.com/watch?v=4jW9wk_g9QY

Page 7: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Aside: Frames in Computer Games

7

In computer animation, the concept of frames is also used as a key abstraction. • In Unity, both the logic and

rendering needed for calculating and displaying a particular image on screen are parts of a frame.

• Unity programs generally display frames at least 30 times per second (fps); often at 60 fps, if possible by the hardware.

• Also, physics and rendering calculations for each frame.

Flowchart of frame logic from the textbook’s Apple Picker Game

Source: Introduction to Game Design, Prototyping, and Development

Page 8: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Unity GameObjects and Components

8

All on-screen elements in Unity are GameObjects, and all GameObjectscontain one or more components• The Inspector shows which Components are

attached to a selected GameObject. • GameObjects are the fundamental objects

in Unity that represent characters, props and scenery.

• A GameObject is a container for many different Components, which are the nuts & bolts of objects and their behaviors.

• The behavior of GameObjects is controlled by the Components that are attached to them.

• Scripts attached to GameObjects are also components.

A Cube GameObject and its attached Components

Source: Unity Technologies

Page 9: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Example Components

9

Transform: Position, Rotation, and Scale

MeshFilter: Holds model data

Renderer: Draws the GameObject model to the screen

Collider: The Physical Presence of the GameObject

Rigidbody: The Physics Simulation

Scripts: Your custom logicSource: Unity Technologies

Page 10: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Transform: Position, Rotation, and Scale

10

The Transform component sets the position, rotation, and scale of a GameObject. • Transform is a mandatory component

that is present on all GameObjects. • Transform is used to store and

manipulate the position, rotation and scale of the object.

• Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. When one object is the child of another, it moves with that parent object as if attached to it.

• Transform can be adjusted by scripts.Manipulating a GameObject’s position

using its attached transformSource: Unity Technologies

Page 11: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

MeshFilter: Manages Mesh Data

11

The Mesh Filter component gives the GameObject its shape, which is modeled as a mesh of triangles.• A mesh is a collection of vertices,

edges, and polygons that describe the shape of a 3D object.

• A vertex is a single point in 3D space. (Plural of vertex is "vertices")

• An edge is a straight line segment connecting two vertices.

• A polygon is a surface enclosed by edges that define the object’s mesh.

• A MeshFilter handles the mesh data for a GameObject, creating a surface that can be rendered.

Example of a triangle mesh representing a dolphin.

Source: Wikipedia

Page 12: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Renderer: Allows You to See the GameObject

12

The Mesh Renderer takes the geometry from the Mesh Filter and draws it to the screen at the position defined by the object’s Transform component• The MeshRenderer requires a

MeshFilter to provide mesh data as well as at least one Material.

• Renderers bring the MeshFilterdata, the Material(s), and lighting together to show the GameObject on screen.

• Real-time rendering of complex geometry is highly processor intensive.

https://www.youtube.com/watch?v=m3pblzmvFpE

Exterior Model for The Unity Adam Demo

Page 13: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Rigidbody: The Physics Simulation

13

Rigidbodies enable your GameObjects to act under the control of physics.• An attached Rigidbody

allows a GameObject to fall under gravity and have physics properties such as mass, drag and velocity.

• To control your Rigidbodies, you will primarily use scripts to add forces or torque. You do this by calling AddForce() and AddTorque() on the object’s Rigidbody.

Note: Manipulating your GameObjects by adding forces to a Rigidbody creates a very different feel and look than adjusting the Transform Component directly.

https://www.youtube.com/watch?v=WTGcs10Sj34

Page 14: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Collider: The Physical Presence of the GameObject

14

Collider components enable a GameObject to interact with other objects in the physics simulation that Unity runs. • Collider components define the

shape of an object for the purposes of physical collisions.

• A collider, which is invisible, need not be the exact same shape as the object’s mesh and in fact, a rough approximation is often more efficient.

• When collisions occur, the Unity physics engine calls methods with specific names on any scripts attached to the objects involved.

The simplest (and least processor-intensive) colliders are the so-called primitive collider types: Box Collider, Sphere Collider and Capsule Collider.

Example collision method (for 3D)

Note: For precision physics, a Mesh Collider can be used; however, mesh colliders are much slower than the other three collider types.

Page 15: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Apple Picker: Homework 02 Overview

15

Atari 2600 Kaboom Game Playhttps://www.youtube.com/watch?v=lwrZHu-d-vY

In this assignment, you’ll recreate parts of the Activision game Kaboom.• In the original game, the player moved buckets back and forth in an

attempt to catch bombs being dropped by a “Mad Bomber.” • In our version, the player uses a basket to collect apples that are falling

from a tree. We’ll create our version from scratch using Unity and C#.

Apple Picker Game Play

Page 16: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Color: A Color with Transparency Information

16

The Color variable type can store information about a color and its transparency (alpha value).• Colors on computers are

additive mixtures of the three primary colors of light: Red, Green, and Blue (or RGB).

• The red, green, and blue components of a color in C# are stored as floats that range from 0.0f to 1.0f

• 0.0f represents none of the color channel and 1.0f represents as much of the color channel as possible.

Note: A fourth float named alpha sets the transparency of the Color. A color with an alpha of 0.0f is fully transparent, and a color with an alpha of 1.0f is fully opaque.

Page 17: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Vector3: A Collection of Three Floats

17

Vector3 is a common data type for working in 3D• A Vector3 is a collection of 3 floats.

• Vector3 is often used to store the 3D position of objects in in 3D space.

• A position (or point) in 3D space is described using three coordinates, labeled x, y, and z.

• Vector3 can also be used to represent a quantity having direction as well as magnitude.

• In this case, Vector3 can be used in many mathematical operations, such as calculating a movement offset or applying a force to GameObjects that are affected by gravity.

A Vector3 describing a position

A Vector3 describing a direction and magnitude

Page 18: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Vector3: Examples of Common Variables and Methods

18

Page 19: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Apple Picker: Basic Gameplay

19

Overview• The player controls the three baskets and

is able to move them left and right using the mouse.

• The apple tree moves back and forth while randomly dropping apples.

• The player must catch the apples using her baskets before they hit the ground.

• A point is awarded for each apple that the player catches, but if a single apple hits the ground, it and all other remaining apples will disappear, and the player will lose a basket.

• When the player loses all three baskets, the game is over.

Apple Picker Game Play

Page 20: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Basket flowchart

Apple Picker: Basic Gameplay

20

Apple Picker GameObjects:• Baskets: Controlled by the player, the

Baskets move left and right following the player’s mouse movements. When a Basket hits an Apple, the Apple is caught, and the player gains points.

• AppleTree: Moves left and right while randomly dropping Apples.

• Apples: Fall straight down from the drop point. Apples that collide with the basket disappear (awarding points). If an apple hits the floor, all apples disappear, and the bottom-most basket is destroyed.

Source: Introduction to Game Design, Prototyping, and Development

Page 21: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Apple Picker: Basic Gameplay

21Apple flowchart Apple tree flowchartSource: Introduction to Game Design, Prototyping, and Development

Page 22: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Apple Picker: Art Assets

22

For this project, we’ll create custom art assets within Unity. • It is possible to create simple 2D and

3D shapes within Unity itself.• Creating this type of programmer art

is a good way to quickly explore various gameplay options.

• These type of assets are generally placeholders that can be replaced with higher quality assets later, which are created by a designer or downloaded from the Unity assets store.

• For this project, programmer art is all that’s required, although you are free to use asset store art if you like.

The book specifies shape transforms using “P” for position, “R” for rotation,

and “S” for scale.

Page 23: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Assignment Summary

23http://learning.ambientcomp.org/2017-multimedia/rJ945/

Homework 02 Due

Homework 02 Assigned

Note: Although Homework 02 is relatively straightforward, it can take a significant

amount of time to complete. I recommend starting this assignment early!

Page 24: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Gamification - Engaging, Motivating and Guiding the World

24https://www.youtube.com/watch?v=oSMKmUZ52yY

Page 25: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Discussion

25

FitBit

Nike FuelBand

Page 26: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Twitter

Discussion

26

TurboTax FourSquare

Page 27: Professor Dr.-Ing. Darren Carlson Department of Electrical ...learning.ambientcomp.org/2017-multimedia/rJ945/slides/06-Game... · Professor Dr.-Ing. Darren Carlson Department of Electrical

Professor Dr.-Ing. Darren CarlsonDepartment of Electrical Engineering | Academy for Creative MediaUniversity of Hawai'i at Mānoa

Discussion

27