- Home /
Collision detection with Physics.Capsulecast is very spotty
Im working on a better type of collision detection for my capsule player, because a regular line raycast is very unreliable on slopes and stairs. I decided to implement a capsulecast for collisions and it worked, but only sometimes. My isGrounded bool changes from true to false pretty randomly depending on where I look with my FPS player.
private void grounded()
{
if (isGrounded)
hitdistance = 1f;
else
hitdistance = 1f;
if (Physics.CapsuleCast(transform.position + (transform.up * 0.5f), transform.position + (transform.up * -.5f), 0.5f, -transform.up, hitdistance, layer))
isGrounded = true;
else
isGrounded = false;
}
the changing hitdistance is from a tutorial on Youtube, not sure if it is neccassary.
I'm confused as to what the script is for is the for collisions between player and stairs, player and ground
if its the player and the ground just attach a character controller component to it and use the charactercontroller.isgrounded property.
Your making it a lot harder on yourself by doing it all from hand hope this gives you an idea :)
Sorry I never got a notification on this comment lol. The script is for being able to jump on stairs and slopes, because raycast goes in the middle of the player, and does not detect the sides of the player. Character Controller had a really basic problem where the gravity would be applied to the character nonstop, making its degree of usefulness drop to 0 for any project besides a top down shooter. I already fixed my problem though, I just made a trigger on the feet of a player character, and whenever anything comes into its collision it just sets an isgrounded bool to true, then i can jump, and when there is no collisions it is not grounded, very simple. Thanks for response, but I already knew about charactercontroller.
Answer by Bingo675 · Nov 04, 2018 at 07:48 PM
I know this post is old, but I had the same problem and solved it by simply extending the raycast which checks for isGrounded a tiny bit further than the raycast which bumps the player up.
So, first check for isGrounded (in order to set your gravity, ability to jump, etc.):
if (Physics.Raycast(headPoint, Vector3.down, out groundHit, myHeight + 0.0001f, ignorePlayer))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
Then, later, bump the player up if he's poking through the ground:
if (Physics.Raycast(headPoint, Vector3.down, out groundHit, myHeight, ignorePlayer))
{
transform.position = new Vector3(transform.position.x, (groundHit.point.y + myHeight / 2), transform.position.z);
}