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?
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.
– anon71970056