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 dee7800 · Mar 03, 2014 at 01:33 AM · distance

Why are Raycast showing 0 distance when using multiple Raycast?

I have three raycast going at the ground from one object. The ground is uneven so i am looking for the minimum distance of the three. the first raycast is showing the correct distance while the other two are both showing zero when the object is clearly farther away than that. Can anyone tell why or what i have wrong?

 if (Physics.Raycast(d_ray1, hit_down1) || Physics.Raycast(d_ray2, hit_down2) || Physics.Raycast(d_ray3, hit_down3)){
     if(hit_down1.collider.tag == "enviroment" || hit_down2.collider.tag == "enviroment" || hit_down3.collider.tag == "enviroment") {
 
         var min_dist1=Mathf.Min(hit_down1.distance,hit_down2.distance);
         var min_dist2=Mathf.Min(hit_down2.distance,hit_down3.distance);
         var min_dist=Mathf.Min(min_dist1,min_dist2);
         Debug.Log(hit_down1.distance);
         Debug.Log(hit_down2.distance);
         Debug.Log(hit_down3.distance);
         
         if (min_dist<epsilon2){
             ground=true;    
             var d_error= buffer-min_dist;
             transform.position.y+=d_error;
         }
     }
     
 }
 else {
     ground=false;
 }



Comment
Add comment · Show 4
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 · Mar 03, 2014 at 01:37 AM 1
Share

Any chance that your rays are failing to hit something? That is, you are not checking to make sure that all the raycast succeeded, just that one of the succeeded, but you are using the data from all three as if all three succeeded. Can we see how you define d_ray1, d_ray2, and d_ray3?

avatar image whydoidoit · Mar 03, 2014 at 01:43 AM 1
Share

With Robert on that - might simply be worth doing -

 hit_down1.distance = float.PositiveInfinity;
 hit_down2.distance = float.PositiveInfinity;
 hit_down3.distance = float.PositiveInfinity;

Before you cast to allow you to skip checking the hit state of each ray. ($$anonymous$$athf.$$anonymous$$in will take 3 parameters by the way).

  var $$anonymous$$_dist=$$anonymous$$athf.$$anonymous$$in(hit_down1.distance,hit_down2.distance,hit_down3.distance);
avatar image dee7800 · Mar 03, 2014 at 02:50 AM 0
Share

the rays are defined as in the code below prior to the if statement. Thanks for the tip on the $$anonymous$$ath.$$anonymous$$in function. Other note: the ray that hits is the first one, d_ray1, and if i edit the code to only work on 2 and 3 then d_ray kicks back a distance but 3 still fails to statying at 0. If i add the float.PositiveInfinity; code from whydoidoit, the two rays keep a distance of infinity and dont change similarly to them staying ataying at zero previously.

     var d_ray1 = new Ray (transform.position+Vector3(.5,-.5, 0), Vector3.down);    
     var d_ray2 = new Ray (transform.position+Vector3(0,-.5, 0), Vector3.down);    
     var d_ray3 = new Ray (transform.position+Vector3(-.5,-.5, 0), Vector3.down);

  
avatar image whydoidoit · Mar 03, 2014 at 02:59 AM 0
Share

Oh sorry - I know what your problem is. An if stops evaluating its terms as soon as possible. It's only going to do the first one that returns true - then it will stop bothering with the rest as it knows it will continue to run its body irrespective of their return values.

1 Reply

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

Answer by whydoidoit · Mar 03, 2014 at 03:00 AM

An if stops evaluating its terms as soon as possible. It's only going to do the first one that returns true - then it will stop bothering with the rest as it knows it will continue to run its body irrespective of their return values. If you always want all three then I suggest you remove the if and check whether minDist is set.

Comment
Add comment · Show 3 · 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 dee7800 · Mar 03, 2014 at 03:25 AM 0
Share

yeah that was the problem. i didn't know it worked like that (still just learning and $$anonymous$$ching myself).

which made me think about the other if statement looking for the tag wasn't so useful as is because i added another object with a different tag partially in the way and it stop on that because even though the first raycast sees the ground the third one was the shortest distance even though it wasn't seeing the ground. I guess that where the layer mask should be useful or to use and's at that point.

thanks for the help.

avatar image whydoidoit · Mar 03, 2014 at 04:23 AM 0
Share

It's actually very helpful that it does work that way because you can incorporate null checks into an "if" without splitting lines:

   if(rigidbody != null && rigidbody.velocity.sqr$$anonymous$$agnitude < 1) { ...

Which would be a null reference exception if it evaluated every term and rigidbody was indeed null.

avatar image robertbu · Mar 03, 2014 at 04:27 AM 0
Share

I missed the short-circuit boolean. Nice catch. +1.

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

21 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

Related Questions

Vector3.Project, how to? 2 Answers

How to write a script to distance meassurment in a terrain 0 Answers

Least heavy processing detection method 1 Answer

Terrain Tree view distance 1 Answer

Distance in the shadows of the sprites and quads. 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