Hello! I want to make an action where a tile changes when pressing E, and the button can only be pressed while it’s 1 block near or smaller.
The problem is, the animation happens before I Press E, but rather when I’m 1 block close or less… (AND some tiles still place before pressing E)
Edit: Also, how do I change multiple tiles instead of just 1?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class ButtonPressed : MonoBehaviour
{
public Tile highlightTile;
public Tilemap Map;
public Animator anim;
private Vector3Int previous;
private GameObject player;
public Grid grid;
void Start() { player = GameObject.Find("Player");
anim = GetComponent<Animator>();
}
void Update()
{
if ((player.transform.position-this.transform.position).sqrMagnitude<1*1)
{
Vector3Int currentCell = grid.WorldToCell(player.transform.position);
// the player is within a radius of 1 unit to this game object
if (Input.GetKeyDown(KeyCode.E))
currentCell.x -=7;
currentCell.y -=5;
Map.SetTile(currentCell, highlightTile);
anim.Play("button");
}
}
}
Edit 2:
I managed to place on multiple tiles, problem is that animation still plays before pressing E.