Intersection of two box-colliders causing buggy movement.

I’ve tried so many different solutions to this to no avail. Basically, whenever I hold right on a wall, I have two triggers on the left and right sides of the player that should prevent them from being able to move into a wall. However, whenever there are two walls with an overlapping edge like so:9654950--1373912--Screenshot 2024-02-20 141509.png
It causes the player to either:

  1. Not move like they should.
  2. Momentarily phase into the wall when the collider leaves the first wall.
  3. Hit a corner and rotate wildly or get stuck.

A video demonstration is available here (the issue is more persistent towards the end of the video):

And to clarify a few things: Disabling rotation is not an option, there are puzzles and platforming sections of the game that utilize the physics of rotation. Combining the walls into one collider or one game object is not always an option, certain platforms have different properties. Primarily, you can’t jump or walljump on grey platforms and there are certain parts where grey and white platforms are flush like in the image above.

I’ve tried a lot of different solutions and would really appreciate it if someone who is better versed in unity’s 2D physics can help me out here. I plan on launching this game on steam and this bug is simply game-breaking and infuriating at certain points of the game.

Lets see some code. Are you manipulating the transform of the player or using physics? The code will help uncover most issues. Use code tags please

Sorry, sideways movement is done by manipulating the transform, not by using the physics system. Upwards movement of a jump is applied using rigidbody physics with continuous collision detection.

void Move(int dir)
    {
        bool canMove = false;
        float delta = movespeed * Time.deltaTime; //Change in x pos next frame.
        switch(dir)
        {
            case 1: //Right
                spi.flipX = true;
                if(!rightColliderCheck.isTouching)
                    canMove = true;
                break;
            case -1: //Left
                spi.flipX = false;
                if(!leftColliderCheck.isTouching)
                    canMove = true;
                break;
            default:
                Debug.LogError("Invalid direction");
                break;
        }
        if(Time.deltaTime > .1f) //Cannot move during stutter > 100ms
            canMove = false;
        if(canMove)
        {
            Accelerate(maxSpeed);
            transform.position = new Vector3(transform.position.x + delta * dir, transform.position.y);
        }
    }

And heres some code to make note of with the side colliders. I think the issue may be caused when trigger enter and exit are both ran on the same frame, but I don’t know what order they’re ran in.

void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag != "Trigger" && collision.gameObject.tag != "Hazards")
        {
            isTouching = true;
            release = false;
            releaseWait.Reset();
        }
    }
    void OnTriggerStay2D(Collider2D collision)
    {
        if(collision.gameObject.tag != "Trigger" && collision.gameObject.tag != "Hazards")
        {
            isTouching = true;
        }
    }
    void OnTriggerExit2D(Collider2D collision)
    {
        if(collision.gameObject.tag != "Trigger" && collision.gameObject.tag != "Hazards")
        {
            isTouching = false;
        }
    }

You really shouldn’t mix together non-rigidbody movement and rigidbody movement. This causes a number of issues with physics and collision detection.

It really just sounds like you should be using physics-based movement here to use the built in Box2D physics.

By modifying the Transform directly you are essentially teleporting things instantly (they don’t move through space)
causing overlaps which the physics system has to solve for any Dynamic colliders.

Thats about what I feared. I wasn’t exactly experienced with unity when I first developed my player controls, but I ended up with something that felt good so I didn’t want to change it. I’ll try to see what I can do by manipulating the rigidbody’s velocity without impacting the game feel too much. Thank you.

As the others mentioned, stick to using physics. Change the rigidbody velocity or use rigidbody.MovePosition(), one (or a combo) of those will get you the results you want.

Got excited when I looked into rigidbody.MovePosition as it seemed to be the closest to replicating the feel the game already had, but it unfortunately doesn’t work very well on a dynamic rigidbody as it seems to freeze the y velocity until I let go of the left or right key.

I played with manipulating the velocity and eventually got something that maintained the game’s feel for the most part. While I’m not too happy with the solution as it results in some smaller bugs I’ll have to tweak later, it does appear to remedy the issue 99% of the time, which I am happy with. So, thank you so much for the help.

It doesn’t “freeze the Y velocity”. It cannot simultaneously move to a specific positions AND move using its currently velocity. That’s like asking for something to move up and down at the same time.

If you ask to move to a position then it’ll do that during the next simulation step. It doesn’t use it’s current velocity and doesn’t use any drag you may have specified. If you don’t ask for any futher MovePosition then it’ll use its velocity/drag for subsequent simulation steps.

If you are continually asking a body to move to a position then each simulation step it’ll do what you ask. It won’t use any velocity it might have because it cannot do that AND move to a position.