Scale / Increase number based on range between two numbers.

139 Views Asked by At

I'm working on a Unity project and have trouble getting my AI Npcs to aim at players in the manner I want them to. This isn't something I need help with, I'm purely curious on how to do my intended solution with math, not whether the approach is a good idea or not.

Currently, I have them linearly move based on a fixed percent. I.e. 50%, this means I calculate the angle between the AI view and the player, and rotate their view angle 50% of that.

Players move very fast in my game, so the AI needs to essentially be aiming very quickly the closer it is to the target, in order to "stay" on target.

What I'm looking to solve mathematically, and the theme of the question is, given the following scenario, how can I increase/ramp up that 50% to 100% based on the remaining FOV (sorta like the difference in angle).

So the maximum FOV is 1

The step is 50%, i.e. cut the angle in half

The minimum FOV the AI can reach is 0.1 before it stops aiming to avoid over locking on the player.

Given X as the minumum (0.10) and Y as the maximum (1.0) how can I scale the percentage Z from it's current value (which can be between [0,100] to 100 based on how close F is to X.

So the current is 0.43, I know this can be be between 0.1 and 1, and I want to scale that 50% number to 100% the closer the current is to 0.1. I hope I explained this clearly enough, but here's an example:

F = 0.98

X = 0.1

Y = 1

Z = 50

In this scenario I'd like Z to become 51 or so.

F = 0.75

X = 0.1

Y = 1

Z = 80

In this scenario I'd like Z to become something like 82-83

F = 0.35

X = 0.1

Y = 1

Z = 40

In this case I'd like Z to be ~70-80.

1

There are 1 best solutions below

0
On

A friend helped me with this:

var factor = (1 - ((fov - MenuSettings.StickyFOV) / (1 - MenuSettings.StickyFOV))) + 1;

Converted to what I said above, this is:

factor = (1 - ((F - X) / (Y - X))) + 1 (I add one here so it becomes a multiplicative factor i.e. I want to make Z 1.2x higher.