- Home /
How to ensure projectiles don't bump into each other
Pretty much what the title says. I'm making a side-scrolling "Mega-man" like game, but the problem is that the projectiles, if shot quick enough, bump into each other and start flying through the Z-Axis. How do I make the projectiles ignore these collisions?
I've tried this script attached to my projectile...
function OnCollisionStay(collision : Collision) {
if (collision.gameObject.tag == "projectile") {
Physics.IgnoreCollision(collision.collider, collider);
}
}
and tagged my projectile prefab with "projectile", but it doesn't seem to work.
Any help would be greatly appreciated.
Answer by qJake · Mar 23, 2010 at 06:01 AM
Call Physics.IgnoreCollision()
inside of Start()
, not when the projectiles collide. IgnoreCollision()
is a preventative measure, not a reactionary one.
Answer by Kith · Mar 23, 2010 at 06:29 AM
function Start () { var projectiles = GameObject.FindGameObjectsWithTag ("projectile"); for (var projectile in projectiles) { if(!(projectile.collider == collider)) Physics.IgnoreCollision(projectile.collider, collider); }
Destroy(gameObject, 7);
rigidbody.AddRelativeForce (Vector3.right * (speed));
}
Thanks! This is the code that fixed my problem. I thought that you called IgnoreCollision right when you first detect the collision, but it makes more sense for it to be reactionary. Thanks again :-).