Explosion parameter

Hi guys,

How can you make an explosion in 2D strong in the center and weaker outside?

I have a landmine, it blows up, its great!
It enables a circle collider 2D for 0.5f seconds, then disappears.
at the moment it is only inflicting 3 hit points on my useless zombies.
But how can I make it that if the zombs are in the center they are dead (Again)
and the ones on the far outside only get -1 retracted on their hit points. Is this built in to colliders?
Or do you need to script this from the scratches?

You’ll want to measure the distance between the zombie and the center of the explosion. The latter is probably just the position of the explosion collider.

You can then do some math based on that distance. I would suggest something like this:

var distance = Vector3.Distance(zombie.transform.position, explosion.transform.position);
var factor = Mathf.InverseLerp(0, explosion.range, distance);
var damage = Mathf.Lerp(-10, -1, factor);

You’ll probably need to tweak some of those variables – I presume this is going to be happening in the OnTriggerEnter() method, but I’m not sure if you have that on the explosion or on the zombie!

InverseLerp takes a range and tells you where a value falls between them. Some examples:

  • 3 is 0.3 between 0 and 10
  • 6 is 0.25 between 5 and 8
  • -1 is 0.5 between 0 and -2

Lerp takes a range and tells you the value at a certain point (called t). Some examples:

  • t=0.5 between 0 and 5 is 2.5
  • t=1 between 5 and 7 is 7
  • t=0.5 between -3 and -5 is -4

As the names suggest, they’re the opposite of each other!

2 Likes

Ok, was thinking i could do it that way but then thought maybe something with colliders, but yeah, i will go along with that, thanks!