• 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
Question by boddole · Nov 24, 2014 at 06:09 AM · c#raycastray

Getting the Furthest Point on a Ray

Hello everyone, I'm trying to find a way of directly getting the furthest point along a Ray.

What I'm doing:

-I'm making a raycast with a max distance, checking to make sure a hit.point is far enough away from the origin, then instantiating a prefab within the ray distance points (randomized min/max use of ray.GetPoint). I've seen that if there is no "hit" the hit.point value will remain as it was, and I can set it to some obviously fake value and check against that (therefore max distance must have been achieved if no object blocked it).

This works okay, but I'd rather just find the furthest point along the ray if possible (and I've seen that GetPoint() will return any value along the ray's direction, so it isn't reliable to check against).

Any ideas are appreciated, and if its not possible or does not make sense, telling me that works to. Thanks.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by tanoshimi · Nov 24, 2014 at 06:46 AM

Your question is a little unclear...

  • The furthest point along a Ray is determined by the distance property specified in its constructor.

  • Or, do you mean the point at which the furthest collision occurs? You could do that with RayCastAll, sorting the results and picking the furthest hit point based on Vector3.Distance.

  • Or, do you mean the furthest the ray travels before it hits something? For that just use regular RayCast, and check the Vector3.Distance of the hit.point (if no collider was hit, RayCast will return false).

Comment
boddole
Crakker1337

People who like this

2 Show 6 · 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 · Nov 24, 2014 at 07:36 AM 0
Share

@ tanoshimi:

-I'll try again with pictures, perhaps it will help:

Basically, when the raycast does hit something, I can just check the distance between the hit.point and the ray.origion.

However when no collision occurs, the only way I can check this is to see if hit.point is still some pre-defined value like (1000,1000,1000), a number that is not possible in normal gameplay. Therefore, I can safely use the max distance of the ray knowing that it will be a safe value.

Ideally I would just like to find the termination point of the ray, without needing another variable to check instead.

It does work as is, and just may not be worth worrying about at this point...

alt text

alt text

no collision.jpg (79.2 kB)
with collision.jpg (92.0 kB)
avatar image sevensixtytwo · Nov 24, 2014 at 07:54 AM 0
Share
 endpoint = ray.origin + (ray.direction * maxRayLength);
avatar image boddole · Nov 24, 2014 at 09:35 AM 0
Share

@ agosevensixtytwo:

-Right, but when there is a collision, we are right be to where we started, since the ray is less than its max length...

avatar image fafase · Nov 24, 2014 at 09:41 AM 2
Share

A ray is mathematically defined by a start point and a direction. It means it has no end, it just keeps on going. Unity adds a new factor so that you can limit the length of the ray which should actually be called a segment since it no longer considers the mathematic requirements of a ray.

So either your ray goes on infinitely or it has been given a length, and that length is the end of the ray.

To get the end of the ray, if you got a hit:

 Vector3 endOfRay = hit.point;

if you got no hit:

 Vector3 endOfRay = ray.origin + ray.direction * length;

 Vector3 endOfRay = ray.GetPoint(length);

or

 indefine since infinite.
avatar image Bunny83 · Nov 25, 2014 at 03:36 AM 1
Share

@boddole:
Raycast returns if you hit something or not. So like fafase said you usually have a setup like this:

 Vector3 endPoint;
 if (Physics.Raycast(yourRay, out hit, yourDistance))
     endPoint = hit.point;
 else
     endPoint = yourRay.GetPoint(yourDistance);

Or if you don't use a Ray it would be:

 Vector3 endPoint;
 if (Physics.Raycast(yourOrigin, yourDirection, out hit, yourDistance))
     endPoint = hit.point;
 else
     endPoint = yourOrigin + yourDirection.normalized*yourDistance;


Also like fafase said if you haven't specified a length for your raycast if's infinite so there is no endpoint unless you specify what should be your max distance.

Show more comments

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Finding Distance between Angles and Points 2 Answers

C# Raycast 2D GameObject follow Mouse 1 Answer

C# Check Physics.Raycast Once 0 Answers

Raycast stops when false, even in update() 1 Answer

C# More Accurate or Larger Raycast 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