- Home /

# 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.

Morning,

for finding the the point along the arc, is it from a fixed origin, like a sweep?

is the vector being normalised, and then multiplied by the distance to get end point?

not at home PC at present, just thinking out loud :)

Not sure what you are asking. If you are looking for the position where a Ray intersects a plane, then use Unity's mathematical Plane class. You can then use Plane.Raycast() to find the hit point. This is a mathematical class, so no mesh is involved. You may also want to take a look at the Math3d class in the Unity Wiki.

Not sure what you are asking

Basically, I would like to find the point along a ray at a set amount of forward distance regardless of the angle of the ray (where a point would be along a ray at the specific world distance, not ray forward distance). Maybe that makes more sense?

You can then use Plane.Raycast() to find the hit point

I'll take a look, thanks.

for finding the the point along the arc, is it from a fixed origin, like a sweep? -> What is going on is that 4 Vector3s are taken as input (the anchor points of the near clip plane, a direction is determined for each point, the ray (which I think uses a normalized vector for its direction parameter) and then a Vector3 point is taken from a float distance moving along the ray. The distance is just applied from the direction vector * the distance float value.

All that brown line shows is how "crooked" the mesh becomes when built, since steeper angles don't go as far as the shallow angles.

**Answer** by SparkyAllMighty
·
Dec 03, 2014 at 02:33 PM

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

->After going over some trigonometry I'm using (with an arbitrary angle of 30): cos(30) = 1000/hypotenuse. It seems to be giving correct values. Am I using the correct formula at this point?

**Answer** by Eck
·
Dec 03, 2014 at 03:07 PM

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.

@Eck:

->I'll try explaning another way:

I'm looking for a ray of any angle to return a point that contains my specified Z value (so if the ray is 1 degree, it should return a point value of: (some value, some value, 1000), if the ray is 75 degrees, it would again be: (some value, some value, 1000).

Using your and Sparky's posts (using an arbitrary angle of 30) I'm doing: cos(30) = 1000/hypotenuse (which seems to give good values).

As a side note, these points are only calculated 1 time and converted into a trigger mesh which is used for the duration of the scene.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Getting the Furthest Point on a Ray 1 Answer

C# Raycast 2D GameObject follow Mouse 1 Answer

Issue with Adding Quaternions 1 Answer

C# Check Physics.Raycast Once 0 Answers

C# More Accurate or Larger Raycast 1 Answer