Can Someone Explain the Sorcery that is Raycasting

So I have 3 stages to a jump: 1) rise, 2) fall, and 3) land. For the land, I want to do a raycast downwards during the fall state to know when the player approaches the ground.

However, I never can understand how raycasting works. I want a short distance (player will only be a tad above the ground when this gets triggered). But the distance value of a raycast is meaningless as it doesn’t change anything.

I can have a distance of 0.0001 set on the array but it’ll still detect collision from 15,000+ units away. Same with distance of 1000. I don’t understand how to shorten the ray.

Furthermore, hit.distance is always 0.

So my question is how can you shorten a raycast?

And if you could be so kind as to explain how raycasting works so I can better understand it, I’d appreciate that as well!

Any help would be greatly appreciated!

You haven’t included any of your code. The Raycast method has like 10 or 12 overloads so it’s very easy to use the wrong overload if you’re not careful. The specified max distance does work correctly. Do you actually check the return values of the Raycast method? If the Raycast method returns false the hit structure won’t contains any meaningful data (just everything is 0). You’re not supposed to read the hit structure unless the method returns true.

If you get a hit you may want to check what you actually hitted. Depending on where you start the raycast and which colliders your player might have it’s possible that your raycast hit some collider / trigger of your player and can never reach the ground, no matter which max length you specified.

That’s also why we have layers and why we can specify a layermask when doing raycasts. So we can selectively specify which layers we want to hit and which we want to ignore. Setting a max distance of “0.0001” certainly doesn’t make much sense. If you want to analyse the hit.distance you probably don’t want to restrict the length of the raycast at all. Just let it cast as long as possible. If it hits something you can check the distance.