Finding Distance between Angles and Points

Hello everyone, I’m having some trouble figuring out how to derive(what type of math I need) to get how much distance is needed to get from an angle to a point (I’ll explain below)

My situation:

I’m building a “area of interest” mesh for some NPC guns (sort of like building a near/far clip plane for a camera). This AOI more/less mirrors their gun traverse limits (so the angles of the traverse values can be different). Through raycasting, I’m creating angles then reading the value of a point along the raycast at the max distance.

Problem in detail:

As a parameter, I have a “max distance” or how far along their Z axis they can see. The problem is that this value decreases as the angle of their traverse increases, resulting in a mesh that is not long enough and is angled.

Ideally, I would be able to get a consistent Z point (max distance) regardless of the ray’s angle (unless that angle is 90 degrees or some other value that doesn’t make sense). Hopefully my picture will give you a better idea of what I’m after.

I have considered using a plane with a trigger and just having the rays give me the hit.point (but I’d rather not involve more objects than is needed). And of course there would be a problem of each gun that makes a mesh hitting other gun’s plane triggers…bit of a mess.

Any ideas are appreciated, thank you for reading.

You want to use trigonometry. Cos, Tan, Sin and the like, which should be in the math helper functions. I’ve not installed Unity yet so can’t be sure.

You know the distance of one side (1000) and presumably you know the angle you are raycasting to. That should give you enough to work out the distance to the point you want.


Replace A with your angle and you should be able to use trig to find out X

Like others, I’m not sure what you’re asking either. Let me know if this idea is correct. Are you asking for where the rays will hit on a flat plane that is perpendicular to a ray straight out from your avatar? If so, that’s just trigonometry.

The length of the 0 ray (your adjacent side ) is going to be your desired z distance (1000 you said). Let’s say your sweep angle is 30 degrees. The length of your the opposite side is going to be

oppositeSide = tan(30)*1000; //= 577.35 (roughly)

You’ve got some unit vectors tied to your game object that are useful.

leftPoint = transform.forward * 1000 - transform.right * oppositeSide + transform.position
rightPoint = transform.forward * 1000 + transform.right * oppositeSide + transform.position

Instead of calculating this every frame, you could make some empty child game objects of your avatar, set their relative position on start, and then as the model moves/rotates it will update the positions update automagically and you can read their position as necessary. Just be aware that if you scale your avatar with a growth effect, that would also change your distance.

Edit: After seeing SparkyAllMighty’s answer, using the hypotenuse might be better. I’m not sure which would be more efficient.

hypotenuse = 1000f / cos(30); //= 866.03 (roughly)

And then find the final resting point by getting your two rays’ unit vector and multiplying by the hypotenuse. If you already have the unit vectors, this is probably faster.