0% found this document useful (0 votes)
83 views7 pages

17bce0918 Da5 PDF

The document provides instructions for implementing collision systems in Unity. It describes how to add collider and rigidbody components to game objects to enable collisions. It explains the different collision events that can be detected by scripts, such as OnCollisionEnter and OnTriggerEnter. It also discusses accessing collision information and filtering collisions between layers or specific game objects.

Uploaded by

Penchal Doggala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views7 pages

17bce0918 Da5 PDF

The document provides instructions for implementing collision systems in Unity. It describes how to add collider and rigidbody components to game objects to enable collisions. It explains the different collision events that can be detected by scripts, such as OnCollisionEnter and OnTriggerEnter. It also discusses accessing collision information and filtering collisions between layers or specific game objects.

Uploaded by

Penchal Doggala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

GAME PROGRAMMING-CSE3029

LAB ASSIGNMENT-5

NAME:D.PENCHAL REDDY SLOT:L49+L50


REGISTRATION NUMBER:17BCE0918 FACULTY:NATARAJAN P

Implement using Unity :


9.Working with Collision systems.
10.Texture basics shaded models importing, normal mapping, Bump mapping.(Mixamo).

Working with Collision systems.


Aim:
We ’ re going to cover adding collider and rigid body components to your game objects, how to
detect and react to collisions between them, and how to access collision data by script and listen
to events triggered by the physics engine.
When creating a new game object in the Unity editor, we can add new components either in the
top menu or in the Inspector, after selecting the related object. Colliders are located under the
Physics tab.

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;

public class CollisionTutorialTest : MonoBehaviour {

void OnCollisionEnter(Collision collisionInfo)


{
print("Detected collision between " + gameObject.name + " and " + collisionInfo.collider.name);

print("There are " + collisionInfo.contacts.Length + " point(s) of contacts"); print("Their


relative velocity is " + collisionInfo.relativeVelocity); }

void OnCollisionStay(Collision collisionInfo)


{
print(gameObject.name + " and " + collisionInfo.collider.name + " are still
colliding");
}

void OnCollisionExit(Collision collisionInfo)


{
print(gameObject.name + " and " + collisionInfo.collider.name + " are no longer
colliding");
}
}
As you can see, each of these methods supply a Collision object, which contains info about the
contact points, the object it collided with, their relative velocity etc. We use it to print to the
terminal information about the collision between the two game objects.

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 OnTriggerExit(Collider other)


{
print(gameObject.name + " and trigger object " + other.name + " are no longer
colliding");
}
As before, if you don’t need info about the other collider, you can leave it empty and speed things
up a bit.

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:

void OnCollisionEnter(Collision collisionInfo)


{
if(collisionInfo.collider.tag == "Enemy")
{
print("Lose health point");
}
else if (collisionInfo.collider.tag == "Powerup")
{
print("Collect powerup");
}
}

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy