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 colliders and triggers Richard Gesick

2 Objectives Unity colliders Unity triggers OnTrigger OnCollision
Destroying game objects

3 Colliders A collider is a component that can be added to a game object
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 and indistinguishable in gameplay. The simplest (and least processor-intensive) colliders are the so-called primitive collider types. In 3D, these are the Box Collider , Sphere Collider and Capsule Collider. In 2D, you can use the Box Collider 2D and Circle Collider 2D. Any number of these can be added to a single object to create compound colliders.

4 Colliders (2) With careful positioning and sizing, compound colliders can often approximate the shape of an object quite well while keeping a low processor overhead. There are some cases, where compound colliders are not accurate enough. In 3D, you can use Mesh Colliders to match the shape of the object’s mesh exactly. These colliders are much more processor-intensive than primitive types, however, so use them sparingly to maintain good performance.

5 Colliders (3) Colliders can be added to an object without a Rigidbody component to create floors, walls and other motionless elements of a scene. These are referred to as static colliders. Colliders on an object that does have a Rigidbody are known as dynamic colliders. Static colliders can interact with dynamic colliders but since they don’t have a Rigidbody, they will not move in response to collisions.

6 Triggers The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. You can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts

7 Collisions OnCollisionEnter(Collision col)
OnTriggerEnter(Collider col) When collisions occur, the physics engine calls functions with specific names on any scripts attached to the objects involved. You can place any code you like in these functions to respond to the collision event

8 OnTrigger sample private void OnTriggerEnter(Collider other) { if (this.tag=="Finish" && other.tag == "Player") go.SetActive(false); if(go2!=null) go2.SetActive(false); win = true; } if (this.tag=="Respawn" && other.tag=="Player") go.transform.position = startPos; go.GetComponent<Rigidbody>().Sleep(); . . .

9 Destroying game objects
The first instinct of many programmers is when a bullet hits an enemy or an enemy hits a player to destroy the object or objects. You need to carefully consider that event. Why? Consider a camera that is a child of a player. What happens when the player is destroyed? In some cases it is better to use player.SetActive(false);


Download ppt "Fundaments of Game Design"

Similar presentations


Ads by Google