- Home /
Disabling collsions between MeshColliders and bullets
I'm looking to disable collision between an instantiated bullet and the object firing it. I looked at a couple previous answers regarding collider disabling, but didn't really manage to make this work.
So I have an object (asteroid with a turret on one side) of the form + GameObject with attached behaviours --Turret 'origin' gameobject (where the weapons fire comes from) --Mesh with a MeshCollider
So the collision between this meshcollider and the shell should be disabled. Here's what I tried:
// Fires a prefab given a position and a direction
function FireWeapon(gunPos:Vector3,gunAim:Quaternion) {
var shell:Rigidbody;
shell = Instantiate (weaponPrefab, gunPos, gunAim);
// need to disable collision between shell and self
var colSelf= GetComponentsInChildren(MeshCollider);
for (var col : MeshCollider in colSelf) {
Physics.IgnoreCollision(shell.collider, col,true);
Physics.IgnoreCollision(col,shell.collider, true);
}
// set the shell speed to what we need
shell.velocity = rb.velocity+turretBarrel.rotation*Vector3(0,0,muzzleSpeed);
}
As you can see, I'm iterating through all the colliders in the children (eventually, shells might be fired through more complex objects, I want to make it robust), and disabling the colliders. However, it's not working. Debugging shows that colSelf
is assigned to the correct collider, and shell.Collider
correctly gives the bullet collider.
I'm kinda stumped! Thanks in advance.
-EDIT: I found some new info - my bullet has a script that kills it when OnCollisionEnter plays:
function OnCollisionEnter(collision: Collision) {
var contact = collision.contacts[0];
var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
PlayHitFX(contact.point,rot);
DoDamageToTarget(contact);
KillProjectile();
}
Disabling this code causes the collision to be ignored. I can't find anything in the documentation about this, but is it possible that IgnoreCollision causes the physics collision to be ignored, but still sends the collision event?
Answer by flaminghairball · Aug 22, 2011 at 07:38 PM
Not sure what your problem is, but you don't need to have two calls to Physics.IgnoreCollision - one will do it. Also, if you're looking for robust behavior, you'll want to change GetComponentsInChildren(MeshCollider) to GetComponentsInChildren(Collider). This will take primitive colliders into account as well.
Thanks! This will help eventually. The two calls are a result of me trying to see if I had to disable collisions in both directions.
Answer by Joshua · Aug 22, 2011 at 06:55 PM
Easiest is probably to set up collision layers. Create the layers you want: bullets and ignorebullets and set them up through Edit > Project Settings > Physics.
I tried this method at first, and it does work well for one enemy and one player, but if I add more enemies, I need to start having layers for "collides with player, collides with enemy 1, collides with enemy 2", etc. Since I plan on having a couple dozen such objects, it would get really complex really fast.
Your answer
![](https://koobas.hobune.stream/wayback/20220612190138im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Turn off all Colliders from Scene 1 Answer