My AI will spot multiple enemy targets anywhere from a distance of 0 - 100. I am trying to figure out how to convert the distance value to a threat value. I want the enemy targets far away to be assigned a low threat value and the targets closer to have a high threat value.

Enemies targets can be distances of: 0 - 100.

Threat values to assign should be between: 10 - 40 (add in increments of 5 - 10).

Problem 1. How to inverse distance so that big distance number becomes small threat number & small distance number becomes big threat number.

Problem 2. I want the scale reduced to only add threat values between 10 - 40. And not be be linear. So threat values get added in large increments of 5-10.

Any help is appreciated.

Thanks,

```
for (int i = 0; i < list.Count; i++)
{
threatLevel.Add(0f);
float distance = Vector3.Distance(transform.position, list*.transform.position);*
```

_ float threatAmountToAdd = **(do some math on distance here)**;_

_ threatLevel *+= threatAmountToAdd;*_

*}*

Well, it’s not really clear what you mean by “add in increments of 5 - 10”. increments of 5 would be clear, but what’s 5 - 10? So you would allow 5.0123 or 6.753 as well? Or just whole numbers between 5 and 10? (5, 6, 7, 8, 9, 10)? Next unclear thing is “not be be linear”. If it’s not linear you have to have a different interpolation. It’s like saying you want a color but not blue. That doesn’t specify a certain color.

One way is to use a linear interpolation. In any case you first want to normalize the range and invert it:

```
if (distance < 100f)
{
float d = 1f - distance/100f; // linear inverse distance
float threat = 10 + d*30;
}
```

This will result in a threat value between 10 and 40 linearly distributed. If you want to limit the increase to steps of 5 you can do this:

```
float threat = 10 + Mathf.Floor(d*7)*5;
```

This will map the distance linearly like this:

```
//
//distance range | d | threat
---------------------------------------
100.00 - 85.71 | 0.00 - 0.14 | 10
85.71 - 71.43 | 0.14 - 0.28 | 15
71.43 - 57.14 | 0.28 - 0.42 | 20
57.14 - 42.86 | 0.42 - 0.57 | 25
42.86 - 28.57 | 0.57 - 0.71 | 30
28.57 - 14.29 | 0.71 - 0.85 | 35
14.29 - >0.0 | 0.85 - 0.99 | 40
0.0 | 1.00 | 45
```

Note that at an exact distance of 0.0 it would increment to the next value. However this should never happen. Even a distance of 0.000001 will still map to 40.