using UnityEngine; public class ElevationHandler : MonoBehaviour { bool onStairs = false; //Variable to tell the code if the player is on stairs or not //Variables to define my tilemaps/different levels of evelvation int groundMiddle; int groundBottom; int groundTop; void Start() { //Setting up layer identification for tilemaps groundMiddle = LayerMask.NameToLayer("Ground_Middle"); groundBottom = LayerMask.NameToLayer("Ground_Bottom"); groundTop = LayerMask.NameToLayer("Ground_Top"); } //This method is called when my player's collider enters a trigger (my stairs) void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Stairs")) { onStairs = true; } } //This method is called when my player's collider exits a trigger (my stairs) void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Stairs")) { onStairs = false; } } // Update is called once per frame void Update() { if (onStairs) //Avoid collisions with all tilemaps when on stairs (allowing for the player to move up or down stairs to a different tilemap/elevation) { Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), groundMiddle, true); Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), groundBottom, true); Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), groundTop, true); } else //If not on stairs then keep all collisions with tilemaps on, disallowing elevation change { Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), groundMiddle, false); Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), groundBottom, false); Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"), groundTop, false); } } }