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.