Swapping between Two 2D Tile Maps during runtime: Player stuck inside of tilemap?

I’m working on a game where you switch from a shadow realm to a light realm. The core mechanic works by swapping the tilemaps and inverting gravity. The tilemaps are inverse of each other. My goal was that the tilemap collider would push the player out of the tiles when you switch, as i have observed with other box colliders.

What actually is happening is that the player is pushed in between the box colliders

I wrote a simplified script to test this mechanic. In the final, the world will invert only if the player is on the ground and presses the down arrow.

Does anyone know or have experience with an invert game mechanic or what issue I’m having?

Cheers!

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEditor.Tilemaps;
using UnityEngine;
using UnityEngine.Tilemaps;

public class move : MonoBehaviour
{

    private Rigidbody2D rb2D;
    public bool Inversed = false;

    public float horizontalForce = 3;//Not being used
    public float jumpStrength = 5f;
    public GameObject whiteGround;//Game Object of the tilemap for each side respectivly
    public GameObject blackGround;
    // Start is called before the first frame update
    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();

    }

    // Update is called once per frame
    void Update()
    {
        //Manages gravity and enabled colliders
        inversedManager();
        movement();
    }

    void movement()
    {

        if (Input.GetKey("w"))
        {
            rb2D.AddForce(Vector2.up * jumpStrength);
            Inversed = false;
        }
        if (Input.GetKey("s"))
        {
            rb2D.AddForce(-Vector2.up * jumpStrength);
            Inversed = true;
        }
        if (Input.GetKey("a"))
        {
            rb2D.AddForce(-Vector2.right * horizontalForce);
        }
        if (Input.GetKey("d"))
        {
            rb2D.AddForce(Vector2.right * horizontalForce);
        }

    }

    void inversedManager()
    {
        if(Inversed == false)
        {
            //Inverse Gravity           
            rb2D.gravityScale = 1;

            //Physics.gravity = new Vector3(0, 1.0F, 0);
            //Change Floor Coliders
            blackGround.GetComponent<TilemapCollider2D>().enabled = true;
            whiteGround.GetComponent<TilemapCollider2D>().enabled = false;
        }
        else//Inverse
        {
            //Inverse Gravity
            rb2D.gravityScale = -1f;
            //Physics.gravity = new Vector3(0, -1.0F, 0);
            //Change Floor Coliders
            blackGround.GetComponent<TilemapCollider2D>().enabled = false;
            whiteGround.GetComponent<TilemapCollider2D>().enabled = true;
        }
    }
}

On a separate note, when I used rb.addforce() to try and get the player to move left and right, it appeared to have quite a bit of friction with the tile map. Only when I lowered the mass of the player did it begin to move left and right easier. However, lowering the mass messed up my jump. Does anyone know what could be causing this interaction?

This is my first post so sorry if my format is bad.

You’d be better off posting some images showing what you mean rather than code first.

In the end, collision response to overlaps isn’t going to move towards some notional “outside edge” of your tilemap. Collision response will move one collision shape out of overlap of another. For instance, instantly place a CircleCollider2D into the middle of a large array of BoxCollider2D and the circle won’t be moved to the outside edge but move out of each overlap with a box which might end up with it stuck (moving between several) between several boxes. It certainly won’t have any concept of an exterior just because they’ve been placed next to each other.

Whether this is your problem or not isn’t clear but images might help. You should never create a situation where you’re severely overlapped inside a bunch of other colliders though as the solver has no idea what you want to happen; it’s only job is to remove overlap between shapes.

1 Like