Search Unity

My Character Falls Through the Ground Intermittently

Discussion in 'Physics' started by joeyxiong1994, Jul 22, 2019.

  1. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Hi there, I'm creating a 3D ACT game. I'm build the player model with only a Capsule Collider and developed its own Ground Detection, Snap To Ground and Collision Pushback.

    Mostly it works fine all the time, but while falling from high place, my character falls through the ground intermittently and out of level then. I used a plane as the base ground. And I also Clamped the falling speed while it is too high.

    In my Ground Detection and Snap to Ground: I used a SphereCast from my center of CapsuleCollider to shoot Vector3.Down direction to find the current Ground by returning me the RaycastHit. And later then I used a OverlapSphere under my character's transform.position to detect if there's a gameObject's transform == RaycastHit's transform. If they equal to each other, it means I'm just a little bit above the ground, I will detect it's isGrounded and Snap to the RaycastHit.transform.position.y + skinWidth.

    In my Collision Pushback, I used a OverlapCapsule to detect if I'm colliding with any colliders and a little bit get through them, and then I used Physics.ComputePenetration for each collider I'm colliding to calculate the push back vector to apply to my own transform.position, which would be (transform.position += dir * dist, where dir and dist is the out from Physics.ComputePenetration). For collision pushback, I recursively check for a couple of times in one Update loop.

    Currently, my Ground Detection, Snap to Ground, and Collision Pushback is all happened after when I call my FinalMove to my character's transform.position, which means my character moved a distance in one frame, and then I will do ground detection and collision pushback for this new position.

    I'm currently using Update function for all of them. But intermittently, I would fall through the ground for no reason. Anyone could give me suggestion to improve this?

    I posted my codes below:

    Collision Pushback
    upload_2019-7-22_10-48-53.png

    Ground Detection:

    upload_2019-7-22_10-49-35.png


    And order in Update():

    upload_2019-7-22_10-50-16.png
     
  2. davincilab

    davincilab

    Joined:
    Aug 8, 2017
    Posts:
    15
    I faced a similar problem with my 2D character collider. I changed the collision detection from Discrete to "Continuous", which did the trick. IDK how much it is relevant to your problem through.