Capsule Collider Third Person Controller hanging on a ledge issue.
Hello, I am using a standard third-person-controller from the asset store but if I jump on a ledge of a collider the player is stuck hanging on the ledge. Below is a screenshot to explain my problem.
As you can see the capsule collider collides with the cube and the player-controller thinks it is still airborn
I haven't looked or used the standard third person character controller, but does but use a Raycast to check for the ground? If so, it may be shooting the Raycast from the centre of the collider and thus missing the platform/ground.
Yes that happens, but if I change the length of the raycast and it checks grounded as true the problem still occurs
Regardless of the length, if you're standing on the edge of a ledge and the raycast is shooting from the middle of the collider, it will never touch it as it runs parallel to it. If it's that much of a big deal to you, I would suggest writing your own controller ins$$anonymous$$d of using the standard asset controller. Alternatively, you could hack your way through this issue and add a couple more raycasts from the sides of your capsule collider to check for edges. There is another option, but its use would be deter$$anonymous$$ed by your game. You could just check for an enter collision and set grounded to true based off that. Obviously grounded would equal false when the collision exits. I probably wouldn't go with that approach though, I'd try to implement my own system. It's up to you. Goodluck.
Answer by Statement · Nov 23, 2015 at 01:36 PM
If the controller is performing a raycast to test for grounded condition, consider doing a spherecast with the same radius as the collider instead.
I don't know how the controller works, but it could be a solution.
I agree with the spherecast option too. I forgot about that! :)
I have tried the spherecast method, but I dont really understand on how to use it. Also after adding more raycasts to the bottom of the capsule collider the problem still occurs, Below a screenshot where you can see that one of the raycasts collides with the cube and the character should be grounded.
I'll be away for a couple of days most likely. It would be nice if someone want to explain how to use spherecast - otherwise I can get back to OP later.
Correct me if i'm wrong, but if multiple raycasts won't work, why should a spherecast do work?
Quick reply: $$anonymous$$ultiple raycasts may work, but not in all cases. In your specific screenshot, it appear there is an error somewhere. Perhaps the ray starts inside the floor or perhaps your logic of testing each raycast has an error. But even if you fixed that, you would be falling if neither of the raycasts actually hit anything (like standing at the corner of the floor).
Answer by sxnorthrop · May 18, 2021 at 07:39 PM
My old answer was kinda poo, here's a far better one:
[SerializeField] private LayerMask m_GroundCheckLayers = 1 << 0;
[SerializeField, Min(0)] private float m_GroundCheckOffset = 0.1f;
private CapsuleCollider m_CharacterCollider;
private void Awake()
{
m_CharacterCollider = GetComponent<CapsuleCollider>();
}
private bool CheckIsGrounded()
{
const float skinWidth = 0.01f;
var center = transform.position + Vector3.up * m_CharacterCollider.radius;
return Physics.SphereCast(center, m_CharacterCollider.radius - skinWidth, -transform.up, out _, m_GroundCheckOffset + (skinWidth * 2), m_GroundCheckLayers);
}
This will create a sphere cast that's slightly smaller than your capsule collider (m_CharacterCollider) and shoot it downward kinda like this: https://imgur.com/66FyBQF
The part of the yellow arrow that passes the blue line is the ground check offset, you can use this to increase the distance between the capsule and the ground that would still allow the character to be considered "grounded".
If the "CheckIsGrounded" function returns true, your collider is on the ground.