- Home /
How to Change the position of a tile to the player position?
Hello everyone,
I want to make my character pick up a tile when clicked on it. I was able to print the coordination of the tile I'm clicking on but I couldn't make it move to the player position. Here is the code I did so far:
using UnityEngine;
using UnityEngine.Tilemaps;
public class MouseManager : MonoBehaviour
{
private Tilemap tilemap;
private Transform playerPos;
void Start()
{
//You can serialize this and assign it in the editor instead
tilemap = GameObject.Find("Tilemap_Ground").GetComponent<Tilemap>();
playerPos = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int gridPos = tilemap.WorldToCell(mousePos);
Sprite spr = tilemap.GetSprite(gridPos);
if (tilemap.HasTile(gridPos))
Debug.Log(gridPos);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Help with scriptable tiles PLEASE HELP URGENT!!! 1 Answer
Equivelant "set" method for "getInstantiatedObject"? 0 Answers
TileMap GetCellCenter not working 0 Answers
Finding the location of the tile in a tilemap nearest to the player 0 Answers
Tilemap2017 Access position & sprite of specific Tile in new Grid System 2dGame 1 Answer