Question by
unity_3nd7k7OAB9ZtCQ · Sep 22, 2020 at 03:38 PM ·
2dtilemaptileisometricupdate function
What is causing this issue with Tilemap.SetTile and Update method?
In this script, I have a method which creates a tilemap tile underneath the parent GameObject Tilemap.SetTile. As the GameObject moves around the grid, the tile should move with it and delete the previous tile:
using UnityEngine;
using UnityEngine.Tilemaps;
public class CreateObstacleTile : MonoBehaviour
{
public Tile obstacleTile;
public Tilemap obstacleMap;
private Vector3Int previous;
private Vector3Int currentCell;
private void SetObstacleTile()
{
currentCell = obstacleMap.WorldToCell(transform.position);
if (currentCell != previous)
{
//set the new tile
obstacleMap.SetTile(currentCell, obstacleTile);
// erase previous
obstacleMap.SetTile(previous, null);
// save the new position for next frame
previous = currentCell;
Debug.Log(currentCell);
}
}
private void LateUpdate()
{
SetObstacleTile();
}
}
It actually works... for a bit. If the GameObject moves a path 'n' cells in the grid, the script will draw 'n/2' cells every time, then stop working altogether. What's causing this issue? The GameObject movement is being handled in a different component:
void Update()
{
if (Vector3.Distance(transform.position, destination) > 0.01f)
transform.position = Vector3.MoveTowards(transform.position, destination, movementSpeed * Time.deltaTime);
}
}
This might help visualise what's happening: https://giphy.com/gifs/UQ0FudTww98XMbGBTL/fullscreen
Comment