- Home /
Physics.raycast distance not working?
I have a pretty simple script: The user clicks on the screen and I raycast from the point that they click at and report back. Its meant to mimic a touch-based interface.
My problem is that while it happily casts a ray, it seems to ignore my distance parameter. As a result, there are a whole mess of objects I literally can't hit, no matter what I change my distance to. Code is below- I feel like I'm missing something obvious.
RaycastHit hit; Debug.Log("Firing Ray"); Debug.DrawRay(ray.origin, ray.direction * rayDistance, Color.yellow);Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit,rayDistance,collectiblesLayer)){ //distance not working! Debug.Log ("Hit something: " + hit.transform.name); Debug.DrawLine (Camera.main.transform.position, hit.point, Color.red);
checkHit(hit.transform.gameObject); }
The Raycast() looks fine. Are you sure it is the distance? Could you have messed up the layer mask? What happens if you leave off distance off (and therefore use infinity)?
If I leave off the distance like this: Physics.Raycast(ray, out hit)
Then everything is fine. Its definitely not the layer mask, because I can collect about 90% of the things in my scene. Its the 10% that are the farthest away that I can't hit, making me thing its the distance.
I have no idea what I did, but it seems to be working now. $$anonymous$$y suspicion is that maybe the controller was too close to the ground or something and it was clipping when I pointed in a certain direction. That makes no sense, but distance is suddenly working like a charm.
Please post an answer and accept it so the question appears answered. :)
Then you wouldn't $$anonymous$$d closing the question would you? :)
Answer by Meatloaf4 · Feb 22, 2014 at 11:39 AM
Chances are you were having the same problem I was. The raycast is more than likely hitting an invisible collider that you have set up.
Chances are you were having the same problem I was. The raycast is more than likely hitting an invisible collider that you have set up.
Your answer
Follow this Question
Related Questions
Distance from raycast. 1 Answer
Turn in the direction of movement 0 Answers
Mouse plane does not detect height 1 Answer
Is Raycast relatived to screen resolution? 1 Answer
RaycastHit.distance 1 Answer