Check the y-coordinate of P1 and P2, if P1 is below P2 (lower altitude) Physics.CapsuleCast WILL work. If not, everything will go to hell.
http://forum.unity3d.com/threads/130169-Why-CaspsuleCast-is-buggy
I'd also like to keep a remnant of my old answer.
This is a very powerful bit of code (which I dub IterationArray):
var hit : RaycastHit;
var hit2 : RaycastHit;
if(Physics.Raycast(pos - Vector3.up*0.5,dir,hit,Mathf.Infinity,(1 << 0))&& Physics.Raycast(pos - Vector3.up*0.5,-hit.normal,hit2,Mathf.Infinity, (1 << 0))&& (hit.distance < radius + dist || hit2.distance < radius + dist))//find the normal below us and find how close it is
{
if(hit.distance > hit2.distance)
{
hit.distance = hit2.distance - radius;
}
else hit.distance -= radius;
}
Basically, what it's saying is: find the nearest normal below you, then do a raycast along the opposite direction of that normal and find the closest distance. If either of those distances are within a suitable distance, then use that normal and DON'T spherecast because it will just waste CPU and give you a slightly less accurate normal. Furthermore, CapsuleCast/SphereCast are unreliable in that they will not return anything if they start "sweeping" from inside a mesh.
Usually, my collision codes look something like this
if(Physics.Check Capsule) //if there is something occupying the space, then do stuff, otherwise don't bother
{
if(IterationArray) //check if we are on a flat plane first
else if(Physics.SphereCast) //check for a contour or corner of a mesh
}
↧