- Home /
Collision between objects
Hello! I have a little problem. I want to do a javascript for doing something when one object collide with other objects (cubes,hills ecc). Do you have some ideas?
Answer by aldonaletto · Aug 25, 2013 at 04:32 PM
Collisions are detected between CharacterControllers and colliders, or Rigidbodies and colliders. Objects that don't have one of these components don't register collisions well enough (an object with a collider may cause OnCollision events only when hitting a rigidbody that's moving - a still rigidbody enters sleep mode, and ignores simple colliders).
If you add a CharacterController to the object (it's a capsule collider, thus doesn't fit well in many objects), use OnControllerColliderHit to process collisions:
function OnControllerColliderHit(hit: ControllerColliderHit){
if (hit.rigidbody){
print("Hit a rigidbody!");
}
}
You must move a CharacterController with Move or SimpleMove, or collisions won't be detected.
If you add a Rigidbody to the object, use OnCollisionEnter to verify collisions:
OnCollisionEnter(col: Collision){
if (col.rigidbody){
print("Hit another rigidbody!");
}
}
Rigidbodies may have colliders of any shape, but you should move them with AddForce or by setting rigidbody.velocity directly in order to have reliable collision detection.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112936im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move object to raycast point. 3 Answers
how to make a script when an object collides with an object it makes another object rise 2 Answers
Make an object destroy the object it is touching? 2 Answers
How can i make my scene restart once my character has collided with another object? 2 Answers
how to make a script when u collide with an object it disappears 3 Answers