- Home /
Difference between collider and collision.
Hay, I'm new to Unity and I'm getting stuck into programming with javascript. I wanted to learn how to see if 2 GameObjects were colliding. So i looked up collisions in the documentation and it turns out there are collisions and colliders.
What's the difference between them?
Also, how would i use them to work out if 2 cubes were colliding? (both cubes are moving towards each other, but when they collide they go through each other).
Answer by eldaville · Feb 14, 2011 at 05:11 PM
collision has the information about the impact, such as velocity and points of impact.
Colliders are the objects that made the impact.
if you use OnTriggerEnter() you pass as a parameter a Collider if you use OnCollisionEnter() you pass as a parameter a Collision
if you want to use a OnCollisionEnter but want a Collider as a parameter you can get the GameObject related to that "Collision" by
void OnCollisionEnter(Collision collisionInfo)
{
// collisionInfo.gameObject.collider;
}
Answer by Bampf · Feb 14, 2011 at 05:11 PM
Colliders are how the Unity detects collisions. When a collision occurs, the methods OnCollisionEnter, OnCollisionExit, and OnCollisionStay on the objects that have colliders will get called as appropriate.
I recommend reading carefully the rigidbody documentation: http://unity3d.com/support/documentation/Components/comp-DynamicsGroup.html For moving objects, they recommend you use rigidbodies and colliders together. (Static objects that don't move may not need the rigidbody.)
So let's say you have cubes, both of which have a rigidbody and a collider.
If you want your cubes to move through each other, and not hit each other, you will probably want to check "Is Trigger" on the colliders. The physics engine won't then bounce the cubes off each other when the collision occurs. Instead of getting the three messages OnCollisionEnter/OnCollisionExit/OnCollisionStay, you will get OnTriggerEnter/OnTriggerExit/OnTriggerStay.
If you want the cubes to NOT be controlled by physics at all, like gravity, then you will probably want to set "Is Kinematic" on the rigidbodies.
When you say "both of which have a rigidbody and a collider." Does that mean i have to manually add the collider? Or should does an object with a rigidbody automatically get a collider?
See for yourself! Select your cube and look at the Inspector pane. The settings for each component is in a separate collapsable pane. If you created the cube using the menu Game Object-->Create Other, it'll have a box collider by default. You can then add a rigidbody with the menu Component-->Physics-->Rigidbody. You can remove either component as well.