- Home /
Raycast is always returning 1 (for normals)
I'm creating a third person character controller, and I'm adding handling for "sliding" (where the character will slowly fall down a slope). Problem is, the normal of the y-axis is returning 1 regardless of where I stand in my world - whereas it should be returning something between 0 and 1 when I'm on an inclined surface.
My character is currently a capsule (with the Character Controller), and my ramp is currently a box at an angle (with a Box Collider) - have I forgotten something really basic?
RaycastHit hitInfo;
if (Physics.Raycast(transform.position + Vector3.up, Vector3.down, out hitInfo))
{
//I've put debug logs here, to confirm normal.y's value
if (hitInfo.normal.y < 0.9f;) {
slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
// It's never coming in here!!! Q_Q
}
}
Occasionally, while on flat terrain, the normal will randomly spike to numbers between 0.5 and 0.9.
Answer by Loius · Jan 22, 2013 at 08:18 PM
I'd bet your raycast is hitting your capsule, since you're casting from where the capsule's top is (by default). Cast from a little lower.
Would I need to cast it from completely beneath the capsule?
No, center would work fine. Somewhere where it won't hit an outside polygon before it leaves the capsule.
I used Debug.DrawLine(POSITION, hitInfo.point); to see what my ray looked like, and it was drawing from about 0.75 units above the capsules head to 0.25 units within its head (I couldn't tell which way it was pointing, but I'm going to assume down based on the upco$$anonymous$$g solution). I removed the Vector3.up from the position to cast the ray (which in the tutorial was supposedly to do what you say, and move the raycast to the middle. The line I had drawing now was from the middle to the floor, and he now slides down ramps.
So thank you. For some reason my raycast was--by default--casting from the middle? What's up with that?
The tutorial probably bumped the capsule up a unit at some point, or the author didn't know what they were talking about or didn't bother to explain that. Both equally likely. (I don't trust tutorials x_x)
Capsules by default are centered around their transform.