- Home /
IgnorePhysics on BoxCollider at GameObject instantiation
I'm developing a 3D Bomberman clone for educational purposes. Similar to the original, I would like my bombs to ignore the player that created them until they move off, but not other players. For this, I thought to instantiate the bomb, use a setter function to mark its parent, then use Physics.IgnoreCollision to ignore collision until the parent was a certain distance away.
Part of the player's Update function:
if (Input.GetButtonDown("Jump"))
{GameObject Bomb = (GameObject)Resources.Load("Bomb", typeof(GameObject));
GameObject inst = (GameObject) Instantiate (Bomb, transform.position, Quaternion.LookRotation(new Vector3(0,0,0)));
BombScript b = inst.GetComponent<BombScript>();
b.setParent(gameObject);
b.ignoreParent();
}
BombScript's IgnoreParent:
public void ignoreParent(){
Physics.IgnoreCollision(gameObject.collider,parent.collider);
}
I'm guessing what is happening is that IgnoreCollision is only called after the collision is already calculated. The parent object having a rigidbody component, it snaps off by the time IgnoreCollision is called.
Both of them have a BoxCollider enabled by default in the prefab. I have also tried adding in the collider via script in the bomb's Start function, but that did not work either.
I am now temporarily using alternative code where the bomb just receives a collider once the parent is far enough, but this has the problem of allowing other players to pass through bombs partially (as the parent may be somewhat off the bomb when another player tries to move, so their own collider is not sufficient). It isn't that big of a problem, but I am interested in getting the first approach to work. I'm relatively new to working with Unity's physics, so I apologize beforehand for any blatant errors I might be making.