- Home /
How to detect a collision between two certain objects
In my game, I want it so then if there are two certain objects colliding, I can tell my script to do something. I know about OnTriggerEnter and OnCollisionEnter but that is with all colliders. I need it so then I can choose what colliders something will happen with. And if you can, I will need it in JavaScript.
Answer by aldonaletto · Jun 07, 2013 at 01:00 AM
Usually tags are used for this purpose. If the moving object is a rigidbody, use OnCollisionEnter:
function OnCollisionEnter(col: Collision){
if (col.gameObject.tag == "Wall"){
// this rigidbody collided with an object tagged Wall
}
}
Triggers are more suitable for things that you may enter or pass through, like portals, pickup items etc. Usually the moving object is a rigidbody or CharacterController, and the trigger is static. If you want the trigger to be the moving object, add a kinematic rigidbody to it or nothing will happen.
function OnTriggerEnter(other: Collider){
if (other.tag == "Wall"){
// this rigidbody or CharacterController entered a trigger tagged Wall
}
}
Your answer
Follow this Question
Related Questions
Spawning and health script not working with collider? 1 Answer
Line Renderer hit detection? 0 Answers
Detecting collision with a specific game object while being a child 1 Answer
2D Collision 1 Answer