- Home /
Trigger Collision Detection
Hey guys, im reading this book that has an example that says just to establish this function without even its use in the Update() or Start():
void OnTriggerEnter(Collider col){ if(col.gameObject.tag == "Player"){ transform.FindChild("door").SendMessage("DoorCheck"); } }
The door child of the outPost Gameobject is just a door with 3 animations (idle, open, close) and the DoorCheck is the function in the door script that activates the sounds and the animations of the door.
The script i wrote above is attached on the outPost, and the character has no other scripts on it exept the prefab scripts.
The problem is that nothing happents. What can i do?
This video may help : http://www.unity3dstudent.com/2010/07/beginner-b13-trigger-collision-detection/
And the Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
Here is a video series by the same author as the book :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials
A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
in the future, try to remember to format your code block by selecting all the code, and hitting the "101010" button.. I'd fix it for you but I'm afraid to break your link..
as for your question, you will need to make sure your player is tagged as "Player" (note that this is looking at the TAG, not the name of the object), and it sounds like you have childed the door to outPost.. that needs to be named door.. the outPost object also needs a collider attached (and the player too, or a character controller), and the collider must be set to "is trigger". One of the objects will also need a rigidbody (probably the outpost if you are using a character controller), just set that to "is kinimatic"... still with me?
@alucardj has some good links, but missing one crucial one:
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
the collision action matrix at the bottom of the page will help explain what combinations of objects can and cannot send trigger signals.. I've only noticed one slight discrepancy myself, as I've described here:
http://answers.unity3d.com/questions/292387/kinematic-trigger-with-static-collider-no-trigger.html
otherwise the collision matrix is very useful to understand why triggers often don't work!
Thanks for the tip, great advice. I never thought of including the collision matrix in this type of question. I was plowing through the moderation que so this got a generic comment based on the Unity3Dstudent series. But yours should be an answer, or as Fattie would say, zoop it to an answer =]
Answer by Seth-Bergman · Oct 03, 2012 at 06:49 PM
in the future, try to remember to format your code block by selecting all the code, and hitting the "101010" button.. I'd fix it for you but I'm afraid to break your link..
as for your question, you will need to make sure your player is tagged as "Player" (note that this is looking at the TAG, not the name of the object), and it sounds like you have childed the door to outPost.. that needs to be named door.. the outPost object also needs a collider attached (and the player too, or a character controller), and the collider must be set to "is trigger". One of the objects will also need a rigidbody (probably the outpost if you are using a character controller), just set that to "is kinimatic"... still with me?
@alucardj has some good links (in the comment above), but missing one crucial one:
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
the collision action matrix at the bottom of the page will help explain what combinations of objects can and cannot send trigger signals.. I've only noticed one slight discrepancy myself, as I've described here:
http://answers.unity3d.com/questions/292387/kinematic-trigger-with-static-collider-no-trigger.html
otherwise the collision matrix is very useful to understand why triggers often don't work!
(...........ZOOP!)