17bce0918 Da5 PDF
17bce0918 Da5 PDF
LAB ASSIGNMENT-5
The basics ones are Box, Sphere and Capsule collider components.
If you want your object to react to physical collision with other objects and the game world, you ’ ll
need to add a Rigid Body component. A game object with a rigid body will be influenced by gravity
and external forces. From the Inspector, you can set whether you want discrete or continuous
collision detection.
Continuous collision detection is used when dealing with fast moving objects. When using Discrete
collision detection, some collisions may not be detected as they may go through the other object
collider between the time another check is performed. By default, this is set to Discrete and you
should leave it as continuous collision detection may really slow down your game.
You won’t need a rigid body if your object’s collider is set as trigger.
set default values for this under Project Settings – Physics, using the Layer Collision Matrix in the
Inspector:
Every Mono Behaviour attached to the game object will listen for collision with other game objects
in the world. The engine offers multiple methods, depending on the collision stage and type of
collision.
For non-trigger collision, you ’ ll use On Collision Enter, On Collision Exit, and On Collision Stay.
On Collision Enter is called when the game object collider starts touching another game object with
a collider and rigid body attached. While colliding, On Collision Stay will be called once per frame.
Finally, when the collision stops, On Collision Exit will be called.
Here’s an example:
using UnityEngine;
using System.Collections;
If you don ’t need to access collision info, you can leave it empty and it will save some calculations.
void OnCollisionStay()
{
print(gameObject.name + “ collided with another object”);
}
Just note that, if the collision stops as a result of one of the objects being destroyed — using
Destroy() — On Collision Exit won’t be called.
If the other game object ’ s collider is set as Trigger, On Trigger Enter, On Trigger Stay, and On
Trigger Exit will be used instead. They work roughly the same way, but supply the Collider object
directly instead of giving a Collision object:
void OnTriggerEnter(Collider other)
{
print("Collision detected with trigger object " + other.name);
}
void OnTriggerStay(Collider other)
{
print("Still colliding with trigger object " + other.name);
}
void OnTriggerEnter()
{
print("Collision detected with a trigger object");
}
Just remember that, for trigger collision event to be triggered, one of the two colliders needs to
have a rigid body attached.
When dealing with collisions, it’s useful to set different tags for the game objects in your game
world. This way, you can quickly determine how to react with different types of collisions in your
game, whether it’ s a collectable, an enemy or anything else:
Filtering collisions
You can force the collision system to ignore certain type of collisions, either specifying the actual
objects collider or using layers.
To ignore collisions between the game object and another game object you’ll need to use Ignore
Collision, supplying the respective colliders:
Physics.IgnoreCollision(gameObject.collider, anotherGameObject.collider);
Just note that both objects need to be active for it to work. As soon as one of them is deactivated
you’ll need to call Ignore Collision again.
On the other hand, Ignore Layer Collision lets you specify two layers (using their IDs as integers)
and will tell the collision system to ignore collisions between objects of layer1 and layer2.
Physics.IgnoreLayerCollision(1, 2);
You can set default values for this under Project Settings – Physics, using the Layer Collision Matrix
in the Inspector: