Colliding with a vertical object is effecting gravity?

In my project, the player has the ability to effectively wall slide. While a character is in contact with a wall, a frictional force is applied to combat the force of gravity. When one collider is involved, the wall slide works perfectly. However, when the character comes into contact with a second collider during a wall slide, it appears that either gravity begins pulling on the character less, or an upwards force is being applied from some unknown source. A link to a video portraying the issue can be found here.

As an additional note, all walls use the same collider with no physic material applied. The two walls shown in the example are lined up exactly and have the exact same width (check attached images). The relevant code can is here:

    public void OnCollisionEnter2D(Collision2D collision)
    {
        Vector2 normal = collision.GetContact(0).normal;
        SurfaceType surfaceType = Utilities.NormalToSurfaceType(normal, MaxFloorAngle);

        if (collision.gameObject.layer == GameConstants.SurfaceLayer)
        {
            switch (surfaceType)
            {
                case SurfaceType.Floor:
                    Debug.Log("<color=cyan>Floor contact</color>");
                    IsGrounded = true;
                    break;
                case SurfaceType.Wall:
                    Debug.Log("<color=yellow>Wall contact</color>");
                    wallContacts++;
                    if (!HasWallContact)
                    {
                        Debug.Log("<color=red>New wall contact</color>");
                        MitigateFallingForce();
                        WallJumpInfo = new WallJumpInfo(MovementData.wallJumpAngle, normal);
                    }
                    break;
            }

            recentContacts.Add(collision.gameObject, surfaceType);
        }
    }
    public void OnCollisionStay2D(Collision2D collision)
    {
        if (HasWallContact)
            ApplyWallSlideFrictionalForce();        
    }
    private void ApplyWallSlideFrictionalForce()
    {

        Debug.Log($"Y velocity {rbody.velocity.y}");
        if (rbody.velocity.y < 0)
        {
            float gravitationalForce = Physics2D.gravity.y * rbody.mass * rbody.gravityScale;
            float frictionalForce = -gravitationalForce * MovementData.wallSlideFriction;
            rbody.AddForce(Vector2.up * frictionalForce);
        }
    }


I think OnCollisionXXX is being called twice, once for character-vs-wall A and once for character-vs-wall B.

I don’t know what HasWallContact does.

The logic should be something like this:

voif FixedUpdate()
{
         // ...
        isTouchingWall = false; //reset the value
}

void OnCollisionEnter2D(Collision2D collision)
{
          if (CheckWallContact(collision) )
                 isTouchingWall = true;
}

void OnCollisionStay2D(Collision2D collision)
{
         if (CheckWallContact(collision) )
                 isTouchingWall = true;
}
1 Like

Spot on call. Thank you so much! Haven’t resolved the issue yet but at least I know what the issue is now.

8492471--1129949--Unity_mSVPzKF7cL.png

Issue has been resolved. The problem was exactly as you described. I was really pulling my hair out on this one. Thanks again!

1 Like