如何在统一2d中获得玩家面前的瓷砖

  • 本文关键字:玩家 2d unity-game-engine
  • 更新时间 :
  • 英文 :


我正试图在unity 2d tileset中获得玩家周围的所有瓷砖。

当玩家按下U键时,我希望发生什么?我希望获得玩家周围的所有瓷砖,包括玩家下面的瓷砖或他们前面的1块瓷砖。

我正在尝试制作一个农业游戏,当玩家拿出一件物品时,它会在瓷砖地图上突出显示。

请注意,我并不是要求完整的代码,我只是想要什么解决方案可以让我在玩家位置附近获得瓷砖

编辑,通过编辑别人的代码找到了一个解决方案,但我不确定是否有更好的方法,如果没有,我也希望这能根据玩家的旋转来工作,目前它会在玩家上方放置一个瓦片。代码源https://lukashermann.dev/writing/unity-highlight-tile-in-tilemap-on-mousever/

using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Tilemaps;
public class PlayerGrid : MonoBehaviour
{
private Grid grid;
[SerializeField] private Tilemap interactiveMap = null;
[SerializeField] private Tilemap pathMap = null;
[SerializeField] private Tile hoverTile = null;
[SerializeField] private Tile pathTile = null;
public GameObject player;
private Vector3Int previousMousePos = new Vector3Int();
// Start is called before the first frame update
void Start()
{
grid = gameObject.GetComponent<Grid>();
}
// Update is called once per frame
void Update()
{   
var px = Mathf.RoundToInt(player.transform.position.x);
var py = Mathf.RoundToInt(player.transform.position.y);
var tilePos = new Vector3Int(px, py, 0);


if (!tilePos.Equals(previousMousePos))
{
interactiveMap.SetTile(previousMousePos, null); // Remove old hoverTile
interactiveMap.SetTile(tilePos, hoverTile);
previousMousePos = tilePos;
}
// Left mouse click -> add path tile
if (Input.GetMouseButton(0))
{
pathMap.SetTile(tilePos, pathTile);
}
// Right mouse click -> remove path tile
if (Input.GetMouseButton(1))
{
pathMap.SetTile(tilePos, null);
}
}
}

尝试在检查器中使用层字段,您可以在Project Manager中创建新的层,在那里您可以轻松地创建类似于"背景"风景"quot;玩家"前景;。。在这之后,你可以将它们单独分配给你的瓷砖,拥有不同的网格也很重要,否则你将无法将不同的层分配给它,我希望它已经工作了

最新更新