UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

23
UFCEKU-20-3 Web Games Programming Unity 3D Physics Colliders and Object Collisions

Transcript of UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

Page 1: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Unity 3D

Physics Colliders and Object Collisions

Page 2: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Agenda

The Unity ‘Physics Engine’ Creating and Using ‘Colliders’ Using Colliders as 'Triggers’ for Events Updating World Objects via Collision and Trigger Events

Page 3: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

The Unity Physics Engine

• Unity contains the powerful NVIDIA PhysX Physics Engine

• The physics engine provides realistic ‘motion under gravity’ characteristics when applied to objects in the 3D environment

• friction, acceleration, collision reactions based on the chosen physics material, metal, wood, rubber, elastic etc.

• The physics engine also provides basic collision detection between objects via ‘Colliders’

• Objects created in Unity usually have a default collider created when the object is created – e.g. a sphere object would be created with a sphere collider. Objects imported as models would require an appropriate collider to be chosen.

Page 4: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Collision Detection Detect when an world object or element has made

contact with another world object or element Collision with world boundaries e.g. terrain Player collides with world object Player acquires a resource

Collisions must be accurately detected Collision events must be handled by system logic World state changed to reflect changes made by

collisions – update values, acquire energy, add time bonus, communicate with other objects via scripts etc.

Page 5: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

2D Sprite Collisions

Page 6: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

2D Sprite Bounding Boxes

Page 7: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

False Collisions

Sprite boundaries overlap but sprites not colliding

Page 8: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Collision Detection for 3D

Some 3D scenarios may use bounding spheres A bounding sphere is a hypothetical sphere that

completely encompasses an object. For finer-grain collision detection several spheres

could be used on a single object Each would need to be coded to see if a collision had

occurred Most 3D APIs provide some functional collision

detection.

Page 9: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Physics ‘Colliders’ in Unity 3D

• When choosing colliders there is a need to take account of the accuracy of collision detection required versus the computational impact of the chosen collider.

• A box collider would use much less computation than a mesh collider

• Unity also provides a capsule collider, which is the default collider for a First Person Controller.

Page 10: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Example Colliders: Box

Cube Box Collider

Page 11: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Example Colliders: Sphere

Cube Sphere Collider

Page 12: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Example Colliders: Capsule

Cube Capsule Collider

Page 13: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Example Colliders: Mesh

Cube Mesh Collider

Page 14: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Example Colliders: False Collisions

Cube Collider on Sphere False Collision

Page 15: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Example Colliders: Imported Models

Model in Maya

Page 16: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Example Colliders: Models

Box on Mesh Capsule on Mesh Mesh on Mesh

Page 17: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Using Colliders as ‘Colliders’

• The default setting for any collider attached to an object is to restrict the object being passed through be other world objects. The collision event must be handled by a script attached to one or both of the objects involved in the collision

ObjectCollisons.js

Code to handle collisions

Page 18: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Handling Events Initiated by Collisions

This function is attached to the First Person Controller (Capsule Collider)

function OnControllerColliderHit(hit:ControllerColliderHit){

if (hit.collider == GameObject.Find("RedSphere").collider){

Debug.Log("I've hit the Red Sphere");

};

if (hit.collider == GameObject.Find("BlueSphere").collider){

Debug.Log("I've hit the Blue Sphere");

};

};

• Unity has a built in function for detecting collisions:

Page 19: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Tag Names and the Tag Manager

• To reference objects in the collision function each object must have a name (Tag) defined via the Tag Manager

• The tag name used in any script reference must exactly match the tag name defined in the Tag Manager

• So RedSphere is not the same as redSphere

Page 20: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Tag Manager Window

RedSphere named in Tag Manager Element Slot

Page 21: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Using Colliders as ‘Triggers’

• Object collisions may be used to generate events ‘triggers’ which can be used to update logic in the World, generate actions, instantiate (create) new objects or remove unwanted objects from the world. Using triggers is one way for the player to collect items that update values via attached scripts.

ObjectTriggers.js

Code to handle collisions as event triggers.

Objects defined as triggers can be ‘passed through’ by other objects

Page 22: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Handling Events Initiated by Triggers

This function is attached to the First Person Controller (Capsule Collider)

function OnTriggerEnter(collisionInfo:Collider){

if(collisionInfo.gameObject.tag == "RedSphere"){

Debug.Log("I've gone through the Red Sphere!");

}

if(collisionInfo.gameObject.tag == "BlueSphere"){

Debug.Log("I've deleted the Blue Sphere!");

Destroy(collisionInfo.gameObject);

}

}

• Unity has a built in function for detecting collisions as triggers:

Page 23: UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.

UFCEKU-20-3Web Games Programming

Summary

• Unity has a built-in Physics Engine that provides ‘motion-under-gravity’ properties and behaviors for objects with attached physics components

• Basic collision detection is also provided by several default colliders such as Box and Capsule for objects created in Unity. Models with more complex mesh topology must have colliders selected to suit their purpose while considering the computational impact on system performance.

• Unity provides basic functions to detect and handle collision events for both collision reactions and triggers events.

• World objects must be named in the Tag Manager for associated scripts to reference object names accordingly. Tag names are case sensitive and must exactly match their corresponding script references.