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 CookieKirby · Dec 17, 2013 at 01:38 AM · raycastraycastingsurfaceplanetaltitude

How can I find the distance from one object to another using raycasts?

How can I calculate the distance from one object to the surface(NOT center) of another using raycasts, similar to LIDAR (LIght Detection And Ranging, which is basically RADAR with lasers). I want to be able to update the distance with FixedUpdate(). basically, it is an altimeter that i want to use with spherical planets. The object i want to detect my distance from might be blocked by another object, but i cant have any objects on the Ignore-Raycasts list.

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 robertbu · Dec 17, 2013 at 01:58 AM 1
Share

Use Collider.Raycast(). This allows you to raycast against a single specific collider rather than raycast through the scene.

avatar image OP_toss · Dec 17, 2013 at 02:08 AM 0
Share

See my answer to a similiar question HERE

Also @robertbu how do you get so much karma!?!

avatar image CookieKirby · Dec 17, 2013 at 02:57 AM 0
Share

that helps a lot, but i still don't know how to find the distance

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 17, 2013 at 04:15 AM

In order to find the distance to the surface of some object without being disturbed by other objects in between, use Collider.Raycast like @robertbu suggested, and measure the distance from your object to the hit point. Supposing that the script is attached to your object and target is the other object's transform, the code could be like this:

 var target: Transform; // planet transform
 private var distance: float;
 
 function FixedUpdate(){
   // get current position:
   var myPos: Vector3 = rigidbody.position;
   // create ray from this object to the planet center:
   var ray: Ray = new Ray(myPos, target.position - myPos);
   var hit: RaycastHit;
   // do a Raycast that sees only the target collider:
   if (target.collider.Raycast(ray, hit, Mathf.Infinity)){
     // save distance to the surface in variable distance:
     distance = Vector3.Distance(myPos, hit.point);
   }
 }
 
 function OnGUI(){
   // show the distance with one decimal digit:
   GUI.Label(Rect(10,10,120,30), distance.ToString("F1"));
 }

EDITED: The field hit.distance already contains the distance from the hit point, thus you don't actually need to calculate it:

   ...
   if (target.collider.Raycast(ray, hit, Mathf.Infinity)){
     // save distance to the surface in variable distance:
     distance = hit.distance;
   }
 }
Comment
Add comment · Show 2 · 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 OP_toss · Dec 17, 2013 at 08:14 PM 0
Share

This is exactly the answer we put above, save for the fact that we didn't code it for him.

If I understand OP correctly, I believe you need to perform 2 raycasts to get the true distance from a mesh to a mesh.

One raycast from A to B, using B's collider. Another from B to A, using A's collider. Then do a Vector3.distance( hitpointA, hitpointB ) to get the true distance.

avatar image aldonaletto · Dec 18, 2013 at 01:42 AM 0
Share

That's right for finding the distance between two surfaces: do a raycast from the object to the planet, then do a second raycast from the hit point to the object and get its hit.distance. But an actual altimeter measures the distance between itself and a ground reference, thus I suppose that a single raycast will do the job.

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

19 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

Related Questions

Raycast origin overlapping surface of collider counts as a hit 0 Answers

Raycast exit point of collider 0 Answers

Raycasting doesn't hit terrains 0 Answers

What's wrong with my RayCastHit2D? 0 Answers

Another Raycast Issue. 0 Answers


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