- Home /
How to make 2D walls Transparent when the player or enemy is behind it.
I've hit a dead end and tried everything I could think of.
I'm making a 2d Isometric game so I need to make specific walls transparent since I can't rotate the camera. My initial solution was to add an empty GameObject with a Polygon Collider as a child of the wall. When the player/enemy enters the "zone" of the Polygon Collider, the specific wall it's attached to will go invisible. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TriggerChangeOpacityTiles : MonoBehaviour
{
Tilemap m_Renderer;
// Start is called before the first frame update
void Start()
{
//Grabs the Tilemap Component
m_Renderer = GetComponentInParent<Tilemap>();
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Player has entered the trigger");
//Turns object invisible when the player enters its collider space
if (other.gameObject.tag == "Player")
{
m_Renderer.color = new Color(1f, 1f, 1f, 0.4f);
}
}
private void OnTriggerExit2D(Collider2D other)
{
Debug.Log("Player has exited the trigger");
//Turns object visible when player enters its collider space
if (other.gameObject.tag == "Player")
{
m_Renderer.color = new Color(1f, 1f, 1f, 1f);
}
}
}
And it worked... Too well.
Should've seen it coming when I used the Tilemap's Alpha. I know there's a code that allows me to affect a specific tile at the exact coordinates, but I don't wanna do that with every tile. Is there another solution?
Answer by LeFlop2001 · Jan 31, 2020 at 09:48 AM
using UnityEngine;
using UnityEngine.Tilemaps;
public class Test : MonoBehaviour
{
public Tilemap tm;
Vector3Int tileToFade;
private void Update()
{
tm.SetTileFlags(tileToFade, TileFlags.None);
tm.SetColor(tileToFade, Color.Lerp(tm.GetColor(tileToFade),Color.clear,0.1f));
}
}
Instead of using boxcolliders, you could use a script that checks the players position (Vector3(x,y,0)) and the lower tiles (Vector3(x-1,y-1,0)) and applies the code above when those tiles are walls
using UnityEngine;
using UnityEngine.Tilemaps;
public class Test : MonoBehaviour
{
public Tilemap tm;
Vector3Int playerPosition;
Tile wall;
private void Update()
{
int x = playerPosition.x;
int y = playerPosition.y;
if (tm.GetTile(new Vector3Int(x - 1, y - 1, 0)) == wall)
{
tm.SetTileFlags(new Vector3Int(x-1,y-1,0), TileFlags.None);
tm.SetColor(new Vector3Int(x - 1, y - 1, 0), Color.Lerp(tm.GetColor(new Vector3Int(x - 1, y - 1, 0)), Color.clear, 0.1f));
}
if (tm.GetTile(new Vector3Int(x + 1, y - 1, 0)) == wall)
{
tm.SetTileFlags(new Vector3Int(x + 1, y - 1, 0), TileFlags.None);
tm.SetColor(new Vector3Int(x + 1, y - 1, 0), Color.Lerp(tm.GetColor(new Vector3Int(x + 1, y - 1, 0)), Color.clear, 0.1f));
}
}
}
I've tried it, but it doesn't seem to work. The player walks on the wall's adjacent ground tiles but the wall stays opaque.
Do I attach the script to the Wall Tiles, the Ground Tiles, or the Player? Does it check the tile from the Sorting layer or the GameObject tag?
Sadly the code I posted is somewhat a pseudo code and won't work without properly adjusting it. I would write you the actual code but sadly, since you're using an isometric tile map, I couldn't figure out how to convert a world position to the tilemap position. The tile positions saved in the tile map are at an 45 degree offset( if you alter the tile (1,0,0) the tile is at (1,1,0) in world Space). If you were to use a normal tile map this would work(simply attach it to the wall tilemap and drag the player into the playerfield in the inspector)
using UnityEngine;
using UnityEngine.Tilemaps;
public class Test : $$anonymous$$onoBehaviour
{
public Transform player;
public Tilemap tm;
public void Update()
{
tm.RefreshAllTiles();
int x = $$anonymous$$athf.RoundToInt(player.position.x - 0.5f);
int y = $$anonymous$$athf.RoundToInt(player.position.y - 0.5f);
Vector3Int pp = new Vector3Int(x,y-1,0);
tm.SetTileFlags(pp, TileFlags.None);
Debug.Log(tm.GetColor(new Vector3Int(pp.x, pp.y + 1, 0)));
tm.SetColor(pp, Color.clear);
}
}
Your answer
Follow this Question
Related Questions
My character is floating. How do I fix this? 1 Answer
Transparrent tiles not working with tilemap collider 2 Answers
Touch Detection in 2D Game 3 Answers
Tilemap Glitching 0 Answers
Tilemap for mobile 1 Answer