CE318: Games Console Programming

Similar documents
Introduction to scripting with Unity

Blender Notes. Introduction to Digital Modelling and Animation in Design Blender Tutorial - week 9 The Game Engine

Cabri Geometry Application User Guide

Pro/ENGINEER Wildfire 4.0 Basic Design

Solving Simultaneous Equations and Matrices

Basic AutoSketch Manual

Understand the Sketcher workbench of CATIA V5.

Figure 1.1 Vector A and Vector F

CATIA Basic Concepts TABLE OF CONTENTS

Maya 2014 Basic Animation & The Graph Editor

The Dot and Cross Products

Physics 125 Practice Exam #3 Chapters 6-7 Professor Siegel

The Car Tutorial Part 1 Creating a Racing Game for Unity

Copyright 2011 Casa Software Ltd. Centre of Mass

GeoGebra. 10 lessons. Gerrit Stols

Section 1.1. Introduction to R n

A vector is a directed line segment used to represent a vector quantity.

2013 Getting Started Guide

Basic controls of Rhinoceros 3D software

Chapter 1. Creating Sketches in. the Sketch Mode-I. Evaluation chapter. Logon to for more details. Learning Objectives

The first step is to upload the Helicopter images from a strip. 1) Click on Resources > Create Sprite 2) Name it spr_helicopter 3) Click Edit Sprite

4 Manipulating Elements

Gerrit Stols

Rotation: Moment of Inertia and Torque

FREE FALL. Introduction. Reference Young and Freedman, University Physics, 12 th Edition: Chapter 2, section 2.5

Working With Animation: Introduction to Flash

CREATE A 3D MOVIE IN DIRECTOR

Introduction to SketchUp

CATIA Drafting TABLE OF CONTENTS

Vector Algebra II: Scalar and Vector Products

Plotting: Customizing the Graph

Adventure Creator User Manual Page 1. User Manual v1.52. Copyright Chris Burton,

INTRODUCTION TO RENDERING TECHNIQUES

SpaceClaim Introduction Training Session. A SpaceClaim Support Document

Force on Moving Charges in a Magnetic Field

Presents. AccuDraw. Instructor Pam Roberts

Introduction to CATIA V5

Tennessee State University

11.1. Objectives. Component Form of a Vector. Component Form of a Vector. Component Form of a Vector. Vectors and the Geometry of Space

Scripting in Unity3D (vers. 4.2)

TWO-DIMENSIONAL TRANSFORMATION

Mechanics lecture 7 Moment of a force, torque, equilibrium of a body

Chapter 23: Drafting in Worksheet View

Tutorial 1: The Freehand Tools

6. Vectors Scott Surgent (surgent@asu.edu)

Protocol for Microscope Calibration

Geometry of Vectors. 1 Cartesian Coordinates. Carlo Tomasi

PHY231 Section 2, Form A March 22, Which one of the following statements concerning kinetic energy is true?

Intro to 3D Animation Using Blender

Unit 4 Practice Test: Rotational Motion

Introduction to Measurement Tools

Course: 3D Design Title: Deciduous Trees Blender: Version 2.6X Level: Beginning Author; Neal Hirsig (June 2012) Deciduous Trees

13-1. This chapter explains how to use different objects.

F B = ilbsin(f), L x B because we take current i to be a positive quantity. The force FB. L and. B as shown in the Figure below.

SimFonIA Animation Tools V1.0. SCA Extension SimFonIA Character Animator

Physics 201 Homework 8

REFERENCE GUIDE 1. INTRODUCTION

PHYS 211 FINAL FALL 2004 Form A

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

Creating Animated Apps

Chapter 10 Rotational Motion. Copyright 2009 Pearson Education, Inc.

How To Run A Factory I/O On A Microsoft Gpu 2.5 (Sdk) On A Computer Or Microsoft Powerbook 2.3 (Powerpoint) On An Android Computer Or Macbook 2 (Powerstation) On

SketchUp Instructions

Excel -- Creating Charts

Tutorial for laboratory project #2 Using ANSYS Workbench. For Double Pipe Heat Exchanger

Experiment 5: Magnetic Fields of a Bar Magnet and of the Earth

Snap to It with CorelDRAW 12! By Steve Bain

4D Interactive Model Animations

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Introduction to the TI-Nspire CX

Introduction to Google SketchUp (Mac Version)

MovieClip, Button, Graphic, Motion Tween, Classic Motion Tween, Shape Tween, Motion Guide, Masking, Bone Tool, 3D Tool

House Design Tutorial

SolidWorks Implementation Guides. Sketching Concepts

GelAnalyzer 2010 User s manual. Contents

PHY231 Section 1, Form B March 22, 2012

Animations in Creo 3.0

Silent Walk FPS Creator 2 User s Manual

1.0-Scratch Interface 1.1. Valuable Information

Section 9.1 Vectors in Two Dimensions

5. Tutorial. Starting FlashCut CNC

Lecture 07: Work and Kinetic Energy. Physics 2210 Fall Semester 2014

ME Week 11 Introduction to Dynamic Simulation

What s New V 11. Preferences: Parameters: Layout/ Modifications: Reverse mouse scroll wheel zoom direction

mouse (or the option key on Macintosh) and move the mouse. You should see that you are able to zoom into and out of the scene.

Quickstart for Desktop Version

Finite Element Modeling

TABLE OF CONTENTS. INTRODUCTION... 5 Advance Concrete... 5 Where to find information?... 6 INSTALLATION... 7 STARTING ADVANCE CONCRETE...

An introduction to 3D draughting & solid modelling using AutoCAD

Midterm Solutions. mvr = ω f (I wheel + I bullet ) = ω f 2 MR2 + mr 2 ) ω f = v R. 1 + M 2m

TRIGONOMETRY FOR ANIMATION

Editing Common Polygon Boundary in ArcGIS Desktop 9.x

INTERNSHIP REPORT CSC410. Shantanu Chaudhary 2010CS50295

ART 269 3D Animation Fundamental Animation Principles and Procedures in Cinema 4D

Universal Law of Gravitation

STATIC AND KINETIC FRICTION


3D-GIS in the Cloud USER MANUAL. August, 2014

Chapter 6 Work and Energy

Transcription:

CE318: Games Console Programming Lecture 2: 2D Games Diego Perez dperez@essex.ac.uk Office 3A.526 2014/15

Outline 1 2D Vectors 2 2D Sprites 3 2D Physics 4 Managing Player Input 5 Lab Session 2 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 2 / 43

Outline 1 2D Vectors 2 2D Sprites 3 2D Physics 4 Managing Player Input 5 Lab Session 2 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 3 / 43

2D Vectors A vector is a geometric object that has magnitude (or length) and direction. Vectors can be used to represent a point in space or a direction with a magnitude. The magnitude of a vector can be determined by the magnitude attribute. 2D Vectors in Unity have many properties and methods, and all of them can be consulted in the reference: http://docs.unity3d.com/scriptreference/vector2.html Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 4 / 43

2D Vectors & Basic Geometry 2D vectors are composed of two components, (x, y). For vectors starting at the origin, these represent the distance from (0, 0) along the X and Y axis. 1 Vector2 vec = new Vector2 (1,2) ; 2 int x = vec.x; //1 3 int y = vec.y; //2 The length (or magnitude) of a vector is determined by its norm, calculated as magnitude = vec = x 2 + y 2. In Unity, the magnitude is available through Vector2.magnitude attribute. 1 int magnitude = vec. magnitude ; // sqrt (5) = 2.236... Example: a vector could represent the speed at which an object moves. If the vector is (2, 5), the object is moving 2 units per second in the X axis, and 5 units in the Y axis. The magnitude of the vector 2 2 + 5 2 = 29 = 5.385 is the linear speed of the object. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 5 / 43

Operations on Vectors (1/5) Addition: (1, 3) + (2, 2) = (3, 5) a + b b + a Scalar multiplication: x(2, 3) = (2, 3)x = (2x, 3x) 2(2, 3) = (4, 6) Dot product: a b = b a = n i=1 a ib i a b = a b cos(θ) Subtraction: (4, 3) (3, 1) = (1, 2) a b b a Negation: a = (3, 2), a = ( 3, 2) b = (2, 2), b = ( 2, 2) Cross product: a b = a b sin(θ) a b (b a) n is the unit vector perpendicular to the plane containing a and b. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 6 / 43

Operations on Vectors (2/5) Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 7 / 43

Operations on Vectors (3/5) In Unity, you can use the normal operators for adding or subtracting to vectors, and also for multiplying or dividing a vector by a number: 1 Vector2 a = new Vector2 (1,2) ; 2 Vector2 b = new Vector2 (3,4) ; 3 int val = 2; 4 5 Vector2 sum = a + b; // or a-b 6 Vector2 mult = a * val ; // or a/ val A unit vector is any vector with a length of 1; normally unit vectors are used simply to indicate direction. A vector of arbitrary length can be divided by its length to create a unit vector. This is known as normalizing a vector. A vector can be normalized in Unity by calling the function public void Normalize. 1 Vector2 a = new Vector2 (1,2) ; 2 a. Normalize (); 3 Debug. Log (a. magnitude ); // =1 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 8 / 43

Operations on Vectors (4/5) The Dot Product operation (sometimes called the inner product, or, since its result is a scalar, the scalar product) is denoted as a b = a b cos(θ), where a and b are the magnitudes of a and b, and θ is the angle in radians between these two vectors. For normalized vectors, the dot product is 1 if they point in exactly the same direction; -1 if they point in completely opposite directions; and a number in between for other cases (e.g. Dot returns zero if vectors are perpendicular). This is a useful operation that allows to determine the angle between two vectors. This is very useful to determine the amount of rotation needed to face certain position, angle of views, etc. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 9 / 43

Operations on Vectors (5/5) In Unity, you can use Vector2.Dot to calculate the dot product, calculate the angle with Mathf.Acos(), and multiply the radians by Mathf.Rad2Deg to obtain the angle in degrees. 1 Vector2 a = new Vector2 (0,5) ; 2 Vector2 b = new Vector2 (5,0) ; 3 4 a. Normalize (); // For consistency, normalize both vectors... 5 b. Normalize (); //... before calculating its dot product. 6 7 float dotproduct = Vector2. Dot (a, b); 8 Debug. Log (" The dot product is: " + dotproduct ); // = 0 9 10 float anglerad = Mathf. Acos ( dotproduct ); // this is arc cos (x). 11 Debug. Log (" The angle is " + anglerad + " in radians."); // = 1.5708 12 13 float angledeg = anglerad * Mathf. Rad2Deg ; 14 Debug. Log (" The angle is " + angledeg + " in degrees."); // = 90 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 10 / 43

Outline 1 2D Vectors 2 2D Sprites 3 2D Physics 4 Managing Player Input 5 Lab Session 2 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 11 / 43

The 2D Mode * When creating a project in Unity, it s possible to set up the editor for 2D Mode: In this mode, Unity automatically sets some parameters for 2D (such as textures and sprites). Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 12 / 43

The Scene View - 2D Mode The Scene View also has a toggle that switches the view between 2D and 3D. When the 2D view is set, the depth coordinate is ignored, making 2D game development much easier. The camera and the view are locked in an orthographic view: Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 13 / 43

2D Graphics Graphic objects in 2D are known as Sprites. Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. Unity provides a Sprite Editor for manipulating sprites. Of special use is to edit an image to separate it into several components. For instance, to keep the arms, legs and body of a character as separate elements within one image. Sprites are rendered with a Sprite Renderer component rather than the Mesh Renderer used with 3D objects. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 14 / 43

2D Transformations * There are for controls at the top left corner to manipulate sprites in the scene: Drag (Hand icon, shortcut Q): To move around the scene. Translate (Shortcut W): Changes position of a sprite. Rotate (Shortcut E): Rotates a sprite. Scale (Shortcut R): Scales a sprite. The last button (Shortcut T) allows to make all these changes on Scene View as shown. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 15 / 43

The Sprite Type * When an image is imported as a sprite, the Texture Type must be set to Sprite (2D and UI). The Sprite Mode can take two values: Single: The image is composed of 1 only element. Multiple: The image contains multiple elements in the same file. Pixels to Units defines the number of pixels from the sprite that will correspond to 1 unit in world space. Pivot determines the position of the pivot in the sprite. The pivot is the point in the image where the sprite s local coordinate system originates. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 16 / 43

Layers (1/4) * It is possible to organize your game objects in layers. Layers are used: by cameras to render only a part of the screen. by raycasting to selectively ignore colliders. by Lights to illuminate the scene. to determine collisions (Layer-based Collision). to determine the order in which sprites are rendered. To assign a game object to a layer, simply select the layer at the top of the Inspector View, with the game object selected: Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 17 / 43

Layers (2/4) Unity provides some layers by default, but you can create new layers in Edit Project Settings Tags and Layers. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 18 / 43

Layers (3/4) It is possible to specify, for both camera and light components, which layers will be affected by these (Culling Mask selector). By default, all layers are rendered by the camera and illuminated by the lights. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 19 / 43

Layers (4/4) Layer-based collision detection: It is also possible to specify which game objects can collide with which by enabling/disabling collisions between their layers. In the Physics 2D Manager, Edit Project Settings Physics2D. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 20 / 43

The Sprite Renderer * The Sprite Renderer component is in charge of drawing the sprite on the screen. Sprite: The Sprite object to render. Color: Vertex color of the rendered mesh. Material: Material used to render the sprite. Sorting Layer: The layer used to define this sprites overlay priority during rendering. Order in Layer: The overlay priority of this sprite within its layer. Lower numbers are rendered first and subsequent numbers overlay those below. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 21 / 43

Outline 1 2D Vectors 2 2D Sprites 3 2D Physics 4 Managing Player Input 5 Lab Session 2 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 22 / 43

2D Physics Unity has a separate physics engine for handling 2D physics so as to make use of optimizations only available with 2D. The fundamentals in both physics systems (2D and 3D) are equivalent. Most 2D physics components are simply flattened versions of the 3D equivalents, but there are a few exceptions. These components are: Rigidbody 2D. Collider 2D. Hinge Joint 2D. Spring Joint 2D. Distance Joint 2D. 2D versus 3D physics: 2D and 3D physics can be used together (they can be present in the same scene) but they won t interact with each other. There is no concept of depth in 2D: All 2D physics occur in the XY plane, with Z = 0. All physics interactions takes places at Z axis position 0. Game objects in 2D can only move along the XY axis and rotate around the Z axis. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 23 / 43

Mass: mass of the object. Linear Drag: Drag coefficient affecting positional movement. Angular Drag: Drag coefficient affecting rotational movement. Both drag parameters slow the speed of the movement, working against the force that moves the object. Gravity scale: Degree to which the object is affected by gravity. Fixed angle (bool): Can the rigidbody rotate when forces are applied? IsKinematic (bool): Is the rigidbody moved by forces and collisions? See next slide. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 24 / 43 Rigidbody 2D * Attaching a rigidbody 2D component to a game object places the object under the control of the physics 2D engine: it will respond to forces applied through the scripting API.

Kinematic game objects * If a game object is kinematic, the rigidbody does not react to gravity or collisions. A kinematic body is meant to be moved only by changing its transform. This check can be set dynamically on scripts: 1 rigidbody2d. iskinematic = false ; This is typically used to keep an object under non-physical script control most of the time but then switch to physics in a particular situation. For example: a player might normally move by walking (better handled without physics) but then get catapulted into the air by an explosion or strike. Physics can be used to create the catapulting effect if you switch off Is Kinemati just before applying a large force to the object. Note: kinematic bodies may have a collider component attached, and then it will react to collisions. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 25 / 43

Collider 2D (1/3) * 2D Colliders allow game objects to have physical shape in the scene s 2D environment. They define an approximate shape of the object that will be used by the physics engine to determine collisions with other objects. The colliders that can be used with a rigidbody2d are: Circle Collider 2D: A circle with a given position and radius in the local coordinate space of a Sprite. Box Collider 2D: A rectangle with a given position, width and height in the local coordinate space of a Sprite. Important: the rectangle is axis-aligned (edges are parallel to the X or Y axes). Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 26 / 43

Collider 2D (2/3) * Edge Collider 2D: The colliders shape is defined by a freeform edge made of line segments. it can be used to fit the shape of the Sprite graphic with great precision. Also, a good use for this is to make a single solid surface, instead of a series of 2D colliders, even if several sprites are used to create the surface. Polygon Collider 2D: The colliders shape is defined by a freeform edge made of line segments that must completely enclose an area. The Polygon Collider 2D can be edited manually but it is often more convenient to let Unity determine the shape automatically. You can do this by dragging a sprite asset from the Project view onto the Polygon Collider 2D component in the inspector. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 27 / 43

Collider 2D (3/3) * Colliders set as triggers will not participate in physical collisions. If a collision happens with a trigger 2D collider, the following messages will be sent: void OnTriggerEnter2D (Collider2D other): Sent when another object enters a trigger collider attached to the game object. void OnTriggerExit2D (Collider2D other): Sent when another object leaves a trigger collider attached to the game object. void OnTriggerStay2D (Collider2D other): Sent each frame where another object is within a trigger collider attached to the game object. However, a 2D collider not set up as a trigger will participate in physical collisions, sending the following messages: void OnCollisionEnter2D (Collision2D other): Sent when an incoming collider makes contact with this object s collider. void OnCollisionExit2D (Collision2D other): Sent when a collider on another object stops touching the object s collider. void OnCollisionStay2D (Collision2D other): Sent each frame where a collider on another object is touching the object s collider. As with standard collisions, one of the objects must have a rigidbody2d. Typically, the trigger is static and the rigidbody2d moves and goes through it. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 28 / 43

Physics Material 2D * A Physics Material 2D is used to adjust the friction and bounce that occurs between 2D physics objects when they collide. Friction: Coefficient of friction for this collider. Bounciness: The degree to which collisions rebound from the surface. A value of 0 indicates no bounce while a value of 1 indicates a perfect bounce with no loss of energy. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 29 / 43

Hinge Joint 2D The Hinge Joint 2D component allows a Sprite object controlled by rigidbody physics to be attached to a point in space around which it can rotate. The rotation can be left to happen passively (in response to a collision, say) or actively powered by a motor torque provided by the joint itself. The hinge can also be set up with limits to prevent it from making a full rotation. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 30 / 43

Spring Joint 2D The Spring Joint 2D component allows two Sprite objects controlled by rigidbody physics to be attached together as if by a spring. The spring will apply a force along its axis between the two objects, attempting to keep them a certain distance apart. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 31 / 43

Distance Joint 2D The Distance Joint 2D component allows two Sprite objects controlled by rigidbody physics to be attached together to keep them a certain distance apart. Note that unlike the Spring Joint 2D, the Distance Joint applies a hard limit rather than a gradual force to restore the desired distance. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 32 / 43

Physics 2D Settings - The physics 2D Manager The physics 2D Manager allows to edit the global Physics 2D settings that affects all sprites in the game. Edit Project Settings Physics2D. Gravity: The amount of gravity applied to all Rigidbody2D objects. Generally, gravity is only set for the negative direction of the Y-axis. Default Material: The default Physics Material 2D that will be used if none has been assigned to an individual collider. Velocity iterations: The number of iterations made by the physics engine to resolve velocity effects. Higher numbers result in more accurate physics but at the cost of CPU time. Position iterations: The number of iterations made by the physics engine to resolve position changes. Higher numbers result in more accurate physics but at the cost of CPU time. Raycasts Hit Triggers: If enabled, any Raycast that intersects with a Collider marked as a Trigger will return a hit. If disabled, these intersections will not return a hit. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 33 / 43

Outline 1 2D Vectors 2 2D Sprites 3 2D Physics 4 Managing Player Input 5 Lab Session 2 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 34 / 43

The Input Manager (1/2) Unity supports the conventional types of input device used with games (keyboard, joypad, etc) but also the touchscreens and movement-sensing capabilities of mobile devices. The Input Manager is where you define all the different input axes and game actions for your project. To see the Input Manager choose: Edit Project Settings Input. All the axes serve two purposes: Allows to reference by name in scripting. Allow the players to customize the controls. All defined axes will be presented to the player in the game launcher, where they will see its name, detailed description, and default buttons. From here, they will have the option to change any of the buttons defined in the axes. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 35 / 43

The Input Manager (2/2) These are the most relevant settings that can be established for each axes: Name: The string that refers to the axis in the game launcher and through scripting. Positive Button: The button that will send a positive value to the axis. Descriptive Name: A detailed definition of the Positive Button function that is displayed in the game launcher. Negative Button: The button that will send a negative value to the axis. Descriptive Negative Name: A detailed definition of the Negative Button function that is displayed in the game launcher. Gravity, Dead, Snap and Sensitivity: Referred to GetAxis, see later below. Invert: If enabled, the positive buttons will send negative values to the axis, and vice versa. Type: Key / Mouse Button for any kind of buttons. Mouse Movement for mouse delta and scrollwheels. Joystick Axis for analog joystick axes. Window Movement for when the user shakes the window. Axis: Axis of input from the device (joystick, mouse, gamepad, etc.) Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 36 / 43

Conventional Input Every project has the following default input axes when it is created: Horizontal and Vertical are mapped to w, a, s, d and the arrow keys. Fire1, Fire2, Fire3 are mapped to Control, Option (Alt), and Command, respectively. Mouse X and Mouse Y are mapped to the delta of mouse movement. All virtual axes are accessed by their name from the scripts. You can query the current state from a script like this: 1 float value = Input. GetAxis (" Horizontal "); 2 bool value = Input. GetKey ("a"); An axis has a value between 1 and 1. The neutral position is 0. This is the case for joystick input and keyboard input. Mouse Delta and Window Shake Delta are how much the mouse or window moved during the last frame, so it can be larger than 1 or smaller than 1. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 37 / 43

Buttons and Keys In Unity, the GetKey and GetButton family functions are ways of receiving input from keys or joystick buttons via Unity s Input class. GetKey referes to specific keys that can be pressed. The class KeyCode allows to specify concrete keys: 1 bool down = Input. GetKeyDown ( KeyCode. Space ); 2 bool a = Input. GetKeyDown ( KeyCode.A); However, it is recommended to use GetButton instead, as this allows you to access the input controls specified in the Input Manager. GetKey and GetButton families have three members that return a bool depending on the key being pressed or not. Input.GetKeyDown() / Input.GetButtonDown(): When a key or button is pressed, it returns true, only in the first frame when it is pressed. Input.GetKey() / Input.GetButton(): Returns true when the key or button is pressed, during every frame it is maintained pressed (this includes the first frame). It returns false in the frame the key is released. Input.GetKeyUp() / Input.GetButtonUp(): When a key or button is released, it returns true. As the down variant, it only lasts one frame. 1 bool jump = Input. GetButtonDown (" Jump "); Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 38 / 43

GetAxis and GetAxisRaw GetAxis returns a float value between 1 and 1, and GetAxisRaw returns an float with the values 1, 0 and 1 only. With axes, we should consider the Positive (value 1) and Negative (value 1) buttons defined in the Input Manager. 1 float h = Input. GetAxis (" horizontal "); 2 float v = Input. GetAxis (" vertical "); 3 float hr = Input. GetAxisRaw (" horizontal "); 4 float vr = Input. GetAxisRaw (" vertical "); There are four settings for GetAxis(): Gravity affects how fast the value returns to 0 after the button has been released. Sensitivity affects how fast the value arrives at 1 and 1 after the button has been pressed. Dead: Any positive or negative values that are less than this number will register as zero. Useful for joysticks, so you can ignore very small amounts of joystick movement. Snap option (if checked) allows to return 0 if both positive and negative buttons are held simultaneously Nice animation in this video: http://unity3d.com/learn/tutorials/modules/beginner/scripting/get-axis Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 39 / 43

Mouse Mouse input can be detected with the GetAxis and GetButton functions, as the input from the keyboard is managed. However, Unity also allows to detect clicks on a collider or a GUI element. If this element has a script attached, the following family of functions will be called: void OnMouseDown(): called when the user has pressed the mouse button while over the GUIElement or Collider. void OnMouseUp(): when the user has released the mouse button. void OnMouseEnter(): when the mouse entered the GUIElement or Collider. void OnMouseOver(): every frame while the mouse is over the GUIElement or Collider. void OnMouseExit(): when the mouse is not any longer over the GUIElement or Collider. void OnMouseDrag(): when the user has clicked on a GUIElement or Collider and is still holding down the mouse. void OnMouseUpAsButton(): when the mouse is released over the same GUIElement or Collider as it was pressed. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 40 / 43

Game Pads Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 41 / 43

Outline 1 2D Vectors 2 2D Sprites 3 2D Physics 4 Managing Player Input 5 Lab Session 2 Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 42 / 43

Lab Preview During this lab you will get some practice with the following concepts: Basics of 2D games, such as sprites, 2D physics and physic 2D materials. Other general concepts like player input, audio and layers. Import assets from the Unity Asset Store and custom assets. Next lecture: 3D Games. Unity3D Game Architecture (Lecture 2) CE318: Games Console Programming 43 / 43