Unexplainable jump boost when touching wall collider.

As asked here:

The solution is supposedly to add a rigid-body component to all of the existing game-objects with colliders and set them to kinematic, that doesn’t fix it for me. I have no idea what could be causing this, but it’s obviously game breaking and i can’t continue with that bug in the game.
Video (don’t mind the character falling through the collider towards the end :roll_eyes:):
https://imgrush.com/FWZ-bpKK4pF6
Any help appreciated, i’ve pretty much hit a roadblock here.:frowning:

The problem is simply, but it depends on, what kind of method do you use for onGround detection?

There:

    void OnCollisionStay2D(){
        onGround = true;
    }
    void OnCollisionEnter2D(){
        onGround = true;
    }
    void OnCollisionExit2D(){
        onGround = false;
    }

This is the problem, the wall is a collision too, so you’re “on ground” whilst touching a wall.
You should do:

void OnCollisionEnter2D (Collision2D collisionInfo) {
   for (ContactPoint2D c in collisionInfo.contacts[]) {
      if (c.normal = Vector2.up) {
         onGround = true;
         break;
      }
   }
}

void OnCollisionExit2D (Collision2D collisionInfo) {
   for (ContactPoint2D co in collisionInfo.contacts[]) {
      if (co.normal = Vector2.up) {
         onGround = false;
         break;
      }
   }
}

Just set the collision detection to continous dynamic, and you’re done.

Thank’s a lot dude, although i didn’t use that same code but i managed to fix it now :smile:

but how did u fix it

that didn’t work for me

I did it by putting a low friction physics material on a character. But as far as I remember your character then might be sliding around the floor. You’ll have to stop him through code

The underlying principle is that the original code pretty much says we are grounded when we touch which isn’t the desired logic. The second example checks the actual point of collision. There are many ways to “ground” a character. I use a RayCast from which I grab the point and angle to detect when my character is slipping off a slope (and subsequently disable jumping).

Below my method. The (best) offsetY var is dependent on character size, I raycast 1.2 units from it’s center, 0.2 below it’s actual feet. It checks whether what I hit was on the Platform layer (a layer I use for the level layout). It then grabs some angles for later use and returns true whenever an object was detected within range.

private bool CastRayGround()
    {
        float offsetY = 1.2f;
        Ray ray = new Ray(transform.position, Vector3.down);
        RaycastHit[] hits = Physics.RaycastAll(ray);
        foreach (RaycastHit hit in hits)
        {
            if (hit.collider)
            {
                if (hit.collider.gameObject.layer.ToString() == "9")
                {
                    _platformNormal = hit.normal;
                    _platformAngleForward = Vector3.Angle(hit.normal, transform.forward) - 90;
                    _platformAngleBackward = 0 - _platformAngleForward;
                    _platformAngleAbs = Math.Abs(_platformAngleForward);
                    if (Vector3.Distance(transform.position, hit.point) < offsetY)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

I would like some feedback on this method, I feel I should reference the layer in a different way. I know it can be done by name.

The friction method is not ideal because the character might be affected by gravity more, the value of the onGround variable will still be true while the objects touch. Imagine your player steer your character against a wall when falling. During it’s descent the onGround variable will be true until that collision no longer happens. Outside of that notion, low friction would typically be want you’d typically want on walls.