- Home /
Raycast collides on nothing?
Hi, I've googled pretty hard for anyone with my problem, but to no avail. So here goes:
My raycast collides on seemingly nothing. Raycasthit.distance returns "42.62..." and Raycasthit.collider returns "null".
{
RaycastHit hit;
Physics.Raycast(transform.position, direction, out hit);
Debug.DrawRay(transform.position, direction * hit.distance, Color.green, 4f, true);
}
I've also tried increasing the raycast distance "manually" (even though that wouldn't make sense), with
Physics.Raycast(transform.position, direction, out hit, mathf.infinity);
So yeah, what is going on?
Thanks for your time!
EDIT: It seems like I provided too little code. I've figured out that it apparently is relevant to mention that the raycast is made inside of a for loop:
for(int i =0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
RaycastHit hit;
Physics.Raycast(transform.position, direction[i,j], out hit);
}
}
hit.distance works properly (i.e. is able to become infinitely large) as long as hit.distance never has been smaller earlier in the loop.
Example: Physics.Raycast(transform.position, direction[0,0], out hit) is always able to return infinty, whereas Physics.Raycast(transform.position, direction[x-1,y-1], out hit) almost never is (since the raycast is bound to hit something until then).
Answer by JohnnytheRocket · Jul 28, 2014 at 07:42 PM
I managed to find a solution by resetting hit.distance = 1000f; after reading it.
Doesn't make a lot of sense to me but at least it works...
Answer by rutter · Jul 28, 2014 at 06:41 PM
The contents of your RaycastHit are only valid if Physics.Raycast returned true (indicating that it hit something).
There may be some data present, regardless, but it's garbage if the raycast didn't actually hit anything.
Usual structure for a raycast is more like this:
RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit)) {
//hit something!
Debug.DrawRay(transform.position, direction * hit.distance, Color.green, 4f, true);
} else {
//didn't hit anything
Debug.DrawRay(transform.position, direction * 1000f, Color.red, 0.1f, true);
}
Thanks for the speedy answer!
I've edited the question as I didn't provide enough information the first time around.
With that said I opted for my current code as the entire grid of raycasts is a lot more understandable if all of them are drawn.
I see the loop, but it doesn't change my advice. The RaycastHit data is only valid if the call returned true. If you're not checking the return value, you're likely to encounter errors.
Answer by matthew2 · Mar 26, 2017 at 05:53 PM
Although this is late, and this is an unorthodox solution, disable "queries hit triggers" in the physics menu, under "Edit -> Project Settings"
Your answer
Follow this Question
Related Questions
Raycast Not Working 3 Answers
Find gameobjects in a crosshair? 1 Answer
Raycast won't hit Collider 1 Answer
Not getting right value; ScreenPointToRay 0 Answers
help the build button in the build settings is grayed out 0 Answers