- Home /
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;
}
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?
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);
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);
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.
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.
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.
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.
Your answer
Follow this Question
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