- Home /
Physics.CheckCapsule() not working.
OK so i've been trying to get this right for a while now and its just not happening. I have a rigidbody as a player and i want to user the CheckCapsule function to detect whether the player is grounded. My understanding of the function was that if I specify 2 points, a point just below the player and a point in the center of the player, and then specify a radius, it would return true only if those points had come in contact with another object. Here's my code.
centerOfPlayer = transform.position;
bottomOfPlayer = new Vector3(transform.position.x, transform.position.y - (ColliderComponent.radius + 0.01f), transform.position.z);
I then plug these 2 vectors into the function.
bool isGrounded = Physics.CheckCapsule(centerOfPlayer,bottomOfPlayer,ColliderComponent.radius/2);\
For some reason this always returns true. I do have GUI components over the top of my player, could it be that somehow the function thinks its colliding with them?
Any help would be much appreciated. Thanks.
Answer by qazwazza72 · Jun 18, 2013 at 10:27 AM
The CapsuleCheck() was returning true because it was hitting the players collider (duh). I fixed this by adding a Layermask mask variable to my player controller script, setting that to default and moving my player into another layer "Player". Then added the mask to the end of the CheckCapsule() function.
bool isGrounded()
{
return (Physics.CheckCapsule(centerOfPlayer,bottomOfPlayer,ColliderComponent.radius - 0.05f,
mask.value));
}
Answer by robertbu · Jun 18, 2013 at 07:34 AM
The points you are defining are the axis of the capsule. From a side view:
Your two points are the red and green points below. The radius is the radius of the half spheres at the ends. So if you place one of the points just below your character, you are covering a 'radius' distance below the point you specify.
Ok so after taking that in this is what I've got:
centerOfPlayer = transform.position;
centerOfBottomSphere = new Vector3(transform.position.x, transform.position.y - ColliderComponent.radius, transform.position.z);//ColliderComponent = GetComponent<CapsuleCollider>();
bool isGrounded = Physics.CheckCapsule(centerOfPlayer,centerOfBottomSphere ,ColliderComponent.radius +0.01f);
It still just returns true every time.
Do an Physics.OverlapSphere at the center of the second (bottom) point using the same radius. Output the name/tag of all colliders it finds.
If u can make out my hastily drawn diagram, this was the expectation of the above code.
AHH, the SphereOverlay came back with the Players collider being hit. That makes sense. Feel dumb not to have thought of that haha. If there's a quick fix to this situation id love to hear it, otherwise ill look it up myself. Thanks heaps!
Your answer
Follow this Question
Related Questions
Physics - Capsule, Rigidbody - Falls over 1 Answer
RigidBody controller - Where to start? 0 Answers
Softbodies in Unity 3D 1 Answer
Throwing knife doesn't throw correctly 0 Answers
Question on Physic Materials, Rigibodies, and Acelleration 0 Answers