- Home /
Why is this raycast working?
I'm following a tutorial for making a rigidbody character controller and it suggests the following method to test whether the capsule collider is grounded:
Physics.Raycast(transform.position, -transform.up, capsuleCollider.height / 2 + 2);
My capsule collider has height 1.8 (around the height in meters of a typical human). That method call is passing half of the capsule height (presumably 0.9) plus a whole 2 for safety! So presumably we should be able to jump when we are around 2 meters of the ground. But, bizarrely, it works. Repeatedly hitting the jump button, it does not jump again until it touches the ground again. If I increase it to, say, 20, then it finally manages to jump mid air. What's up with this?
Answer by saschandroid · May 31, 2013 at 08:13 PM
This works, because the third value (`capsuleCollider.height / 2 + 2`) describes only the length of the ray that is shot, not how far away your collider is. So it won't hit something that is farer away then 2.9 in your case, but everything in a shorter distance.
Yeah, I understand that, but the strange thing is that while I am mid air but less than 2m above ground, the raycast doesn't seem to detect anything. Only after I hit the ground am I able to jump again. I would have thought that the safety net should be more like 0.2...
Answer by DemianT · May 31, 2013 at 08:58 PM
My bad, it actually was allowing me to jump when I got to within 2 meters of the ground. It's just that for some reason those 2 meters really didn't look like 2 meters.