- Home /
Raycast Distance
I have a ball with radius .15, I am trying to cast a ray forward from its center with distance .2 and if it hits something I want that somethings information. This is the line of code `Physics.Raycast(down.position, down.forward *.2, hit, 1)` it is in an if statement, and it doesn't work. When I press play the ray seems to reach out too far and hit other objects, so either I am doing this wrong(and I have tried the many versions of Raycast) or the scale of the scene is different which I dont think is the case because I draw a ray with all the same properties and it only extends .2. So anyone know Rays better than I do?
EDIT: I've added a picture. The green ray, is what is what the ray is supposed to be doing. The two highlighted objects are gate1 and gate2, and red ray is the result when I try `Debug.DrawRay(hit.point, hit.point, Color.red, 120);` the game seems to think that "hit" in this case is gate2, and completely ignores gate2
Answer by DesiQ · Sep 13, 2012 at 09:25 PM
Rays have infinite length. Multiplying the magnitude of the vector would do nothing, as the ray would be infinite anyway. Use RaycastHit.distance to check whether the collision happened within the distance you wanted. Or use
Physics.Raycast(transform.position, -Vector3.up, hit, 0.2f)
to cast a ray straight down, limiting possible collisions to 0.2 world units only.
Here is the most current itteration of my block, and it still picks up every object: if(Physics.Raycast(down.position, down.forward, hit, 0.2f)){ if(hit.transform.name.Contains("Gate") && hit.distance < .5){ print(hit.transform.name); } }
What exactly are you trying to accomplish, and for what purpose? I am confused because you named your object/reference down, and yet it is actually a ball, and you are trying to fire a ray forward. What is down's type? It's unclear why you're not using
down.transform.position
down.transform.forward
if it's a moveable object. I just tried your code (but using transform.position and transform.forward) on a flycam and had no problems.
Rays are not only infinite in length, but they pass through objects even after colliding. RaycastHit can return an array of all the collisions that the ray has made within the limit distance. Is this what you mean by "picks up every object"?
The note in the documentation of Physics.Raycast might be relevant here:
Note: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour.
Raycast should only return one hit, RaycastAll on the other hand returns all hits.
down is: down = transform.Find("Down"); It's called down because I use it earlier in the code to move the ball its inside down. If the Ray is supposed to be infinite then what is the point of distance? But whats more troubling is that when I try to check hit.distance < .5, it says the gates are, when they clearly are not. Stranger still, there's gate2 gate3 and gate4, the ray will pick up gate3 and not gate2
I down know if this will affect anything but gates are children of the tube. I say this because when I checked print(down.position - hit.point); it printed out (0.0, 0.0, 0.0).
Consider me baffled too, then. $$anonymous$$y advice is to make a new test scene and test your script on a flycam against cubes. It should work (worked for me anyway). Then change the cubes to gates and see if it works. Then swap the flycam out for a ball and see if that works. There shouldn't be anything wrong with that code, so it must be something related to your scene's setup.
Your answer
Follow this Question
Related Questions
RaycastHit.distance 1 Answer
Hit distance Change light range 1 Answer
RTS camera help 1 Answer
Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer