Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by boddole · Dec 03, 2014 at 03:09 AM · c#raycastmath

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. alt text

example.jpg (147.8 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OboShape · Dec 03, 2014 at 07:43 AM 0
Share

$$anonymous$$orning,

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 :)

avatar image robertbu · Dec 03, 2014 at 07:48 AM 0
Share

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 $$anonymous$$ath3d class in the Unity Wiki.

avatar image boddole · Dec 03, 2014 at 10:46 AM 0
Share

@robertbu:

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). $$anonymous$$aybe that makes more sense?

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

  • I'll take a look, thanks.

@OboShape:

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 deter$$anonymous$$ed 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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

alt text

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


trig.png (1.7 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image boddole · Dec 04, 2014 at 03:49 AM 0
Share

@SparkyAll$$anonymous$$ighty :

->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?

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image boddole · Dec 04, 2014 at 03:48 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Check Physics.Raycast Once 0 Answers

C# More Accurate or Larger Raycast 1 Answer

Getting the Furthest Point on a Ray 1 Answer

C# Raycast 2D GameObject follow Mouse 1 Answer

Issue with Adding Quaternions 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges