Math question for my AI

Hi, the photo I drew better explains what I am trying to accomplish. I am trying to adjust a gun barrel to point over a mountain. The closer it is the higher it needs to point and the further away the lower it needs to point as it moves along.

The barrel needs to point between -80 at the closest and -40 at the furthest from the mountain.
The total x axis movement range is -700 to -2000. So at -700 axis it should be -80 and at -2000 it should be -40.

So the total from -700 to -2000 is 1300 steps basically. I divided that by the total range that is -80 to - 40 is 40 steps which gave me 32.5. I got as far as this equation…

…and it gives me the exact reverse of what I needed, which is understandable. So replacing the first number -700 with -2000 returns -80, which I need to be -40. Also the result of -700 is -40, which I need to be minus -80.

I know I am close, I just can’t figure it out. Any help would be appreciated!

No need- if I understand you correctly, just create a Vector3 position in space that you want to point at based on distance / height (or just based on a Transform target you position in the scene), get the current position of the character, and then subtract the latter from the former to get a vector at that angle and a magnitude that’s equivalent to the distance. Set this new vector to Magnitude = 1, and you’ve got a simple direction to point. Alternatively, just use Transform.LookAt to achieve exactly the same thing in a single function call.

I was not expecting that but an interesting approach, but I still need to solve this mathematically.

There are a number of other variables that I need to take in to account and would need to adjust this value depending on wind speed and a couple other factors. But if I am unable to do that then this would be a good place to start. Thanks so much for replying with a great idea!

number of steps / 32.5 - 80
assuming that step 0 = -700

Mathf.Lerp is also a possibility, if you’ve got a minValue, a maxValue, and a percentage (0-1) of where within that range your character is currently standing. So, maybe something like Mathf.Lerp(minAngle, maxAngle, (currentPosition - minPosition) / (maxPosition - minPosition));

My friend, you have solved yet another problem and it was so simple after looking at it. Don’t know why I was looking at advanced algebra youtube videos trying to crack this master equation when it was as easy as that, haha.

So yes -700 to -2000 max positions is 1300 steps. So at step 650 it should be -60 and this is.

This will get me a good starting point for the angle and I modify it from there with wind force and wind direction.

Thanks Lysander, methos5K nailed it and I am getting the results I needed for a starting point! But thank you for the additional help!

Glad I could help :slight_smile: