How to Get a Rigidbody2D with a BoxCollider2D to Move Right Against a Composite Tilemap Collider?

Hello all. I have been having a problem when moving my player along a wall in a top-down RPG style. I’ve been using Unity’s tile map system. When moving along the wall, it works perfectly find except for the fact that the player slows down slightly and the position of the player shows that it cannot actually get all the way against the wall (as shown in video). My physics settings are the default, except I have everything running in fixed update. As well as that, I have a default physics2D material with not friction or bounciness that is applied to everything. My player has continuous collision detection, with interpolate set to interpolate. I’ve tried every solution that I could find, which is the only reason I’m looking for help now.

    public IEnumerator Move(Vector3 moveVec, Action OnMoveOver = null)
    {
        if (PlayerMovement.MyInstance.currentState != PlayerState.paused)
        {
            if (gameObject.GetComponent<CharacterAnimator>() != null)
            {
                animator.MoveX = Mathf.Clamp(moveVec.x, -1f, 1f);
                animator.MoveY = Mathf.Clamp(moveVec.y, -1f, 1f);
            }

            var targetPos = transform.position;
            targetPos.x += moveVec.x;
            targetPos.y += moveVec.y;

            if (!IsPathClear(targetPos))
            {
                if (gameObject.CompareTag("NPC"))
                {
                    PlayerMovement.MyInstance.canWalk = true;
                }

                yield break;
            }

            if (!IsTileClear(targetPos))
            {
                if (gameObject.CompareTag("NPC"))
                {
                    PlayerMovement.MyInstance.canWalk = true;
                }

                yield break;
            }

            if (CompareTag("Player"))
            {
                if (PlayerMovement.MyInstance.currentState != PlayerState.stagger)
                {
                    while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon && GetComponent<PlayerMovement>().currentState != PlayerState.stagger)
                    {
                        IsMoving = true;

                        transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);

                        yield return null;
                    }

                    SetPositionAndSnapToTile(targetPos);

                    IsMoving = false;

                    OnMoveOver?.Invoke();
                }
            }
            else if (CompareTag("NPC"))
            {
                while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
                {
                    if (IsTileClear(targetPos))
                    {
                        IsMoving = true;

                        transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
                    }
                    else
                    {
                        IsMoving = false;
                    }

                    yield return null;
                }

                SetPositionAndSnapToTile(targetPos);

                IsMoving = false;

                if (GetComponent<NPCController>() != null && GetComponent<NPCController>().directionAfterMove != null)
                {
                    FaceDirection(GetComponent<NPCController>().directionAfterMove);
                }

                OnMoveOver?.Invoke();
            }
        }
    }

You’re talking about rigidbodies, but there isn’t anything about a rigidbody in your code.

To elaborate a bit on @spiney199 's answers:

You’re making a typical rookie mistake (and that’s okay; we all need to start somewhere).

If you have collider & a rigidbody on your player game object, you must NOT move its transform.

Using the transform will not involve physics, its like teleporting from point to point. You may think it works, because the physics engine may push your character back when you’re hitting a collider, but that is coincidence.
This is exactly what you see in your video when the character moves down to the wall. It bumps into it (Y = 5) and then gets pushed back by the physics system due to the collision (Y=4.8).

If you want to move a rigidbody, then there are multiple ways.

If it’s a kinematic rigidbody, again: there is no physics involved during movement, you have to calculate it yourself.

If you’re using a dynamic rigidbody, you can move it either via AddForce or via velocity.

In your case I would try to use a dynamic rigidbody and move it via velocity.

1 Like

Excellent advice above.

That said, if you’re making a tile-by-tile pokemon style movement, then I would argue you don’t need rigidbodies/colliders at all.