Transparency on a specific section of the tilemap

Hi!

So I think @Velcronator had asked this question before (see question here), but I’m not sure how the proposed solution helps, so I’d like to ask again:

In the lecture, the transparency script is attached to the Canopy tilemap object. This object also has a tilemap collider. Whenever the player enters the collider, all of the canopy fades.

However, preferably, I only want the actual section of the canopy the player is under to fade, so that other canopy tiles across the scene don’t fade out.

What would be a good way to go about this issue? Thanks!

Image: I don’t want the canopies to the left and bottom of the screen to fade out

That’s true. It wasn’t the right solution. I’ll have s look as well.

1 Like

At a quick guess on a Monday morning i am not sure on the most tidy solution but instinct is telling me that multiple layers of canopy would work.
Pretty sure this is not the most elegant of solutions but it should work

1 Like

So it’s not optimal yet, but I figured something out!

My code (script is attached to my Canopy tilemap):

using UnityEngine;
using UnityEngine.Tilemaps;

public class TransparentTilemap : MonoBehaviour
{
    private Tilemap tilemap;
    private GridLayout tileGrid;

    private void Awake() {
        tilemap = GetComponent<Tilemap>();
        tileGrid = tilemap.layoutGrid;
    }

    private void OnTriggerStay2D(Collider2D other) {
        if(!other.GetComponent<PlayerController>()) return;

        var cell = tileGrid.WorldToCell(other.transform.position);
        var color = tilemap.GetColor(cell);
        tilemap.SetColor(cell, new Color(color.r, color.g, color.b, 0.7f));
    }
}

Side notes:

  1. A cell is (in my project) a lot smaller than a tilemap tile, so I had to wobble my character around a bit to create a bigger transparent area.
  2. I’m not resetting the transparency yet, but I was thinking about tracking transparent tiles in a list. If my characters runs around and any known transparent tile isn’t part of the collided tiles, I could reset its transparency. Again, a bit difficult to do correctly due to the size of a tile compared to a cell.

I’m pretty happy so far! I’ll let you know when I have a new update.

2 Likes

Great work solving your problem

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms