- Home /
Allow Units with Colliders to Go Through Each Other
Im trying to allow units not collide with one another with the tag "unit". Plain and simple, so far i have this but its not working:
charcontroller = GetComponent<CharacterController> ();
//if collides with another unit such as itself
//pass through
void OnCollissionEnter(Collision col){
if (col.transform.tag == "unit") {
Physics.IgnoreCollision (charcontroller, col);
}
}
That doesnt work for an event of two character controllers
Dont you have a hard time making two character controllers colliding in the first place?
Especially without using this
http://docs.unity3d.com/ScriptReference/CharacterController.OnControllerColliderHit.html
Oh yes, the other answer on that page was deleted. It was about the Collision $$anonymous$$atrix.
Answer by maccabbe · Jul 01, 2015 at 05:07 PM
An easy alternative would be to make the units on the same layer and disable collisions between objects in that layer
http://docs.unity3d.com/Manual/class-PhysicsManager.html
http://docs.unity3d.com/Manual/class-Physics2DManager.html
http://docs.unity3d.com/ScriptReference/Physics.IgnoreLayerCollision.html
http://docs.unity3d.com/ScriptReference/Physics2D.IgnoreLayerCollision.html
If you want to use Physics.IgnoreCollision (or Physics.IgnoreLayerCollision) then you should use it before a collision happens. Once OnCollisionEnter is called after a collision is computed and it uses this data. If you want to turn off collisions after the initial one you should make sure you objects are setup correctly so OnCollisionEnter is called.
http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
http://docs.unity3d.com/ScriptReference/Physics2D.IgnoreCollision.html
oh right! and since im using a character controller that means I use OnControllerColliderHit? http://docs.unity3d.com/ScriptReference/CharacterController.OnControllerColliderHit.html
As for layers, I have never used them. Is there a recommended tutorial?