Calculate Controller VIbration Based on Distance from Explosion

Hello,

I have a tunable Unity setting for my grenades called RumbleIntensity (between 0 and 1, typically around 0.5)
I’m trying to come up with a value between 0 and 1 (1 being highest intensity) based on the distance my player is from an explosion in the game.

I have the distance variable, typically it’s around 2 to 6 if within range of the explosion… but I’m trying to get a value between 0 and 1 based off that distance.

Any pointers would be much appreciated.

First you need to normalize the value, and the formula is y = (x - min) / (max - min), where x is the original value and y is the value between 0 and 1. This gives you a value between 0 and 1 but it’s backwards from what you want, because 0 will mean closest to the explosion and 1 will mean farthest. Thus, you need to subtract this value from 1 to get what you actually want. So in your case:

vibration = 1 - (distance - 2) / (6 - 2);

or just

vibration = 1 - (distance - 2) / 4;

This will work as long as the distance value is always between 2 and 6. If it’s sometimes a little bit more or less you can be on the safe side by then doing this:

vibration = Mathf.Clamp01(vibration);

to make it always return a value between 0 and 1. That will mean anything closer than 2 will get full vibration, and anything further than 6 will get zero vibration.