Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Proclyon · Nov 10, 2010 at 09:56 AM · collisioncollisiondetection

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by StephanK · Nov 10, 2010 at 12:19 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Proclyon · Nov 10, 2010 at 12:27 PM 0
Share

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.

avatar image StephanK · Nov 10, 2010 at 12:44 PM 0
Share

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)

avatar image Proclyon · Nov 11, 2010 at 08:39 AM 0
Share

Thank you for the clarification on your answer

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to detect an object inside another object? 1 Answer

How to make my player play a vault animation when i collide into a vaultable object? 1 Answer

In-game pickup not moving towards player after being collected 1 Answer

Problem with OnCollisionStay and user input 2 Answers

Collision Detection Problem 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges