Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundaments of Game Design

Similar presentations


Presentation on theme: "Fundaments of Game Design"— Presentation transcript:

1 Fundaments of Game Design
Unity Movement Richard Gesick

2 Objectives Using rigid bodies Applying forces Applying velocities
Time intervals Key input

3 Rigidbodies Rigidbody is a component that should be added to game objects that are going to be affected by gravity or collisions. “Adding a Rigidbody component to an object will put its motion under the control of Unity's physics engine. Even without adding any code, a Rigidbody object will be pulled downward by gravity and will react to collisions with incoming objects if the right Collider component is also present.“

4 RigidBodies (2) The Rigidbody has a scripting API that lets you apply forces to the object and control it in a physically realistic way The FixedUpdate function is recommended as the place to apply forces and change Rigidbody settings. The reason for this is that physics updates are carried out in measured time steps that don't coincide with the frame update. FixedUpdate is called immediately before each physics update and so any changes made there will be processed directly. Rigidbody api

5 Applying a force to a rigidbody
void FixedUpdate() { float hor = Input.GetAxis("Horizontal"); float ver = Input.GetAxis("Vertical"); Vector3 move = new Vector3(hor, 0.0f, ver); GetComponent<Rigidbody>().AddForce(move * speed * Time.deltaTime); }

6 Using Velocity We can use velocity instead of force but don’t use both. It will cause problems. Create the velocity vector, multiply it by speed and time and then add it to the rigidbody.position or transform.position

7 Using a velocity void FixedUpdate () { float hor = 0; float ver = 0; if (Input.GetKey ("a")) hor = -1; if (Input.GetKey ("d")) hor = 1; if (Input.GetKey ("w")) ver = 1; if (Input.GetKey ("s")) ver = -1; Vector3 vel = new Vector3 (hor, 0, ver); GetComponent<Rigidbody>().position+= vel * speed* Time.deltaTime; //or GetComponent<Transform>().position+=vel*speed* Time.deltaTime; }

8 Key input Input.GetAxis("Horizontal"); is set by unity as the a and d keys as well as the left and right arrows Input.GetAxis(“Vertical"); is set by unity as the w and s keys as well as the up and down arrows Input.GetKey returns true thru each frame as long as the key is down if (Input.GetKey ("a")) Input.GetKeyDown( string name) (or up) returns true just once, in the frame that it is pressed in and doesn’t generate a new value until it has been released and pressed if (Input.GetKeyDown ("d"))

9 Key naming Normal keys: “a”, “b”, “c” … Number keys: “1”, “2”, “3”, …
Arrow keys: “up”, “down”, “left”, “right” Keypad keys: “[1]”, “[2]”, “[3]”, “[+]”, “[equals]” Modifier keys: “right shift”, “left shift”, “right ctrl”, “right alt”, “left alt”, “right cmd”, Mouse Buttons: “mouse 0”, “mouse 1”, “mouse 2”, … Joystick Buttons (from any joystick): “joystick button 0”, “joystick button 1”, Joystick Buttons (from a specific joystick): “joystick 1 button 0”, “joystick 1 Special keys: “backspace”, “tab”, “return”, “escape”, “space”, “delete”, “enter”, “insert”, “home”, “end”, “page up”, “page down” Function keys: “f1”, “f2”, “f3”, … The names used to identify the keys are the same in the scripting interface and the Inspector.


Download ppt "Fundaments of Game Design"

Similar presentations


Ads by Google