Reputation: 43
I am working on a project in unity that is based on tilemaps, and I need to be able to change the entire tilemap at once. I know that I can use SetColor for a single tile but that will require excess code that I would prefer to shorten. My code below is attached to a tilemap that I want to reduce the alpha on when my character is in a certain location. It currently only does this for a single tile with the SetColor function.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class houseController : MonoBehaviour
{
public GameObject player;
public Tilemap house;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
house = GetComponent<Tilemap>();
}
// Update is called once per frame
void Update()
{
Vector3 position = player.transform.position;
Vector3Int x = new Vector3Int(1, 10, 0);
Color color = new Vector4(1, 1, 1, 0.5f);
Color color2 = new Vector4(1, 1, 1, 1);
house.SetTileFlags(x, TileFlags.None);
if (position[0] > -5.3f)
{
if (position[0] < 4.2f)
{
if (position[1] > 12.3f)
{
if (position[1] < 18.2f)
{
house.SetColor(x, color);
}
else {
house.SetColor(x, color2);
}
}
else {
house.SetColor(x, color2);
}
}
else {
house.SetColor(x, color2);
}
}
else {
house.SetColor(x, color2);
}
}
}
Upvotes: 0
Views: 3982
Reputation: 21
It might be better to put a collider set to trigger over the area that the player has to stand, then you can use OnTriggerEnter2D and OnTriggerExit2D to find out when the players in the right spot.
This is my code for fading the colour out from the tiles that the player has previously gone behind hopefully it helps
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
[RequireComponent(typeof(TilemapCollider2D))]
public class TileColourChanger : MonoBehaviour
{
public float lerpDuration = 3f;
public float endAlpha = 0.2f;
private Tilemap tilemap;
void Start()
{
tilemap = GetComponent<Tilemap>();
foreach (Vector3Int tilePosition in tilemap.cellBounds.allPositionsWithin)
{
tilemap.RemoveTileFlags(tilePosition, TileFlags.LockColor);
}
}
void OnTriggerStay2D(Collider2D collider)
{
if (collider.CompareTag("Player"))
{
Vector3 centre = collider.bounds.center;
Vector3 min = collider.bounds.min;
Vector3 max = collider.bounds.max;
Vector3[] corners =
{
new Vector3(min.x, min.y, 0f), new Vector3(min.x, max.y, 0f), new Vector3(min.x, centre.y, 0f),
new Vector3(max.x, min.y, 0f), new Vector3(max.x, max.y, 0f), new Vector3(max.x, centre.y, 0f),
};
foreach (Vector3 corner in corners)
{
Vector3Int tilePosition = tilemap.WorldToCell(corner);
if (tilemap.HasTile(tilePosition))
{
StartCoroutine(LerpColour(tilePosition));
}
}
}
}
IEnumerator LerpColour(Vector3Int tilePosition)
{
Color lerpedColour = tilemap.GetColor(tilePosition);
float timeElapsed = 0;
while (timeElapsed < lerpDuration)
{
lerpedColour = new Color (lerpedColour.r, lerpedColour.g, lerpedColour.b, Mathf.Lerp(1, endAlpha, timeElapsed / lerpDuration));
tilemap.SetColor(tilePosition, lerpedColour);
timeElapsed += Time.deltaTime;
yield return null;
}
lerpedColour = new Color (lerpedColour.r, lerpedColour.g, lerpedColour.b, endAlpha);
tilemap.SetColor(tilePosition, lerpedColour);
tilemap.SetTileFlags(tilePosition, TileFlags.LockColor);
}
}
Upvotes: 2