Before updating to Windows 10 you may need to return your Pro license. More information here

Collision detection general approaches in Unity3D for C#

Hello again Unity3D users and game developers!

First thing I would like to say explaining my situation is that I have been reading about collision detection in Unity3D for several hours. And many of the questions that were posted on the Unity Answers community have already been read once or twice by me now. Please reference all you want, it won't hurt or anything, but don't make that hold you back from attempting to answer yourself please!

OnTriggerEnter seems to evaluate specifically tagged objects ( Unwanted , since I don't feel like tagging 8976216 level items , nor nag the non-programmers here to do so, probably a nice idea for switches but not for floors and walls)

OnCollisionEnter seems to check for interaction with other colliders and rigidbodies.

Currently there are some holes in the whole collision detection system for me. Before I go and learn the collision detection matrix scroll down in link from A-Z, I want to be able to understand the following question:

WHO is calling WHAT, WHEN?

Should I get a controller or rigidbody instantiation inside my script to check what's going on when I want to know if the parent is standing on the floor or bumped his/her head?

Everyone is just mashing the following aswell I noticed

"Unfortunatly in JS"

function OnControllerColliderHit(hit : ControllerColliderHit)
     //Respond to collision

Ok the event is raised somehow? Why build a method with the same name unless it is an override of something inside the UnityEngine imported library or overload for that matter? I can't seem to find any references in any object or library to such methods so I would assume it's homemade. If so, who calls it then, nothing in all the code samples is? Everyone keeps replying to these methods with "It does not work" aswell, so I'm not the only person bugged by this I'm guessing.

All the collision detection methods and events seem really usefull and practical, but not when I can't for the life of me figure how who's controlling them when and how. Even my components in the game never heard of them. (EDIT 4/13/2011 , this is actually good programming practice, the functions within the library only need to know what to do , not where the live or what their neighbours are doing , aka, encapsulation)

Eventually I want to be able to check collisions with the environment from inside the player object using my controller script, without having to tag the entire the world, and WITH at least a basic understanding of what is going on under the hood.

EDIT: 11/11/2010

I tested the following

void OnCollisionEnter(Collision collision) { //get the collision object when a new collision occurs collisionEnter = collision; }

void OnCollisionStay(Collision collisionInfo) { //get the collision object when a collision //that has already occured last frame is still present collisionInfoStay = collisionInfo; }

void OnCollisionExit(Collision collisionInfo) { //get the collision object when a collision was present last frame //but is not present this frame collisionInfoExit = collisionInfo; }

void OnTriggerExit(Collider other) { foo = 1; }

void OnTriggerStay(Collider other) { foo = 2; }

void OnTriggerEnter(Collider other) { foo = 3; }

void OnControllerColliderHit(ControllerColliderHit hit) { //Respond to collision foo = 4; }

I have tested colliding with my character with an RB (RigidBody) attached to collide with all permutations with rigidbodies colliders ( not caring about the shape or size) . All no effect

The results were interesting. Only OnControllerColliderHit actually ever responded. If they were functions called like Update and Start, then they should have responded. So am I missing something in the code or whatnot ? I copy pasted straight from the unity scripting reference C# example btw.

EDIT 11/15/2010:

Apperantly the OnControllerColliderHit only checks if a collision just occured, it will not work as a work around for the slightly more advanced collision detection I need in my product. So I will be needing a bit of help getting the OnTrigger and OnCollision calls done somehow.

EDIT 11/15/2010 (2):

Got just the trigger event working, still nothing on the other 3 though.

more ▼

asked Nov 10, 2010 at 09:56 AM

avatar image

1.6k 31 28 51

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The controlling instance for everything collision related is the physics engine. The engine will call all those collision/trigger functions when it detects a collision. It will call these functions on all gameObjects that are part of a collision. The OnControllerColliderHit function is an overridable function just as Update or Start. To see a list of all those functions look at the scripting docs under MonoBehaviour.

Also it is wrong that you need to tag anything for OnTriggerEnter to happen. It's just common practice to tag certain objects, to have a fast way to determine if you've hit something relevant or just the ground for example.

To see when which method is called you can look at the mentioned collision matrix. In short: A collision happens if at least one of the two "colliding" objects is a dynamic rigidbody and both have some sort of collider (not trigger) attached to them. A trigger event will happen when one of the two objects is a rigidbody (dynamic or kinematic) and both have colliders attached and one of those is a trigger.

more ▼

answered Nov 10, 2010 at 12:19 PM

avatar image

6.6k 119 110 171

How can you not tag something and say it is the ground? Wouldn't that just be making an assumption about the collision?

Thanks for the explanation on the overrideable functions. That's the information I was looking for.

Nov 10, 2010 at 12:27 PM Proclyon

I said it's common practice to do this. If you have a scenario where you don't care what you're hitting or can assume that everything is relevant you don't need to. But the same applies for every collision not only triggers. You don't need to use tags for that, you could also use layermasks for this or check if an object has certain components/scripts attached. (e.g. the ground would probably not have a rigidbody or a CharacterController)

Nov 10, 2010 at 12:44 PM StephanK

Thank you for the clarification on your answer

Nov 11, 2010 at 08:39 AM Proclyon
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Nov 10, 2010 at 09:56 AM

Seen: 17348 times

Last Updated: Apr 13, 2011 at 11:38 AM