Hello!
I am making a 2D game and after a few days of testing, I am unsure how to add force to all colliders within a certain radius. As seen as this is a 2D game, AddExplosionForce cannot be used (maybe it can and I am just missing something).
Maybe use Physics2D.CircleCast
or Physics2D.OverlapCircle
to get array of colliders:
Vector2 originOfExplode;
float radius;
float forceMultiplier; // force of the explosion
public void Explosion ()
{
Collider2D[] colliders = Physics.OverlapCircleAll (originOfExplode, radius);
foreach (Collider2D col in colliders)
{
// the force will be a vector with a direction from origin to collider's position and with a length of 'forceMultiplier'
Vector2 force = (col.transform.position - originOfExplode) * forceMultiplier;
Rigidbody rb = col.transform.GetComponent<Rigidbody> ();
rb.AddForce (force);
}
}