C# 获取网格的中心&x27;s细胞统一
我正在和Unity做一个等距游戏,我需要知道细胞在世界坐标中的中心。我正在使用tilemap的C# 获取网格的中心&x27;s细胞统一,c#,unity3d,game-development,C#,Unity3d,Game Development,我正在和Unity做一个等距游戏,我需要知道细胞在世界坐标中的中心。我正在使用tilemap的GetCellCenterWorld()方法,但它返回了糟糕的结果。 我所期望的是单元的中心(基本上是逻辑中心),但Unity所返回的是什么(单元的顶部顶点?)。我相信这与精灵的轴心点(现在设置为其顶部中心)或tilemap属性中的单元锚点(我将其设置为(1,1))有关 下面是我用来“选择”瓷砖的代码: 公共类GridSelection:MonoBehavior { 公共Tilemap Tilemap;
GetCellCenterWorld()
方法,但它返回了糟糕的结果。
我所期望的是单元的中心(基本上是逻辑中心),但Unity所返回的是什么(单元的顶部顶点?)。我相信这与精灵的轴心点(现在设置为其顶部中心)或tilemap属性中的单元锚点(我将其设置为(1,1))有关
下面是我用来“选择”瓷砖的代码:
公共类GridSelection:MonoBehavior
{
公共Tilemap Tilemap;
公共变换选择器;
无效更新()
{
Vector3 worldPos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
worldPos.z=0;
向量3int tilemapPos=tilemap.WorldToCell(worldPos);
selector.position=tilemap.GetCellCenterWorld(tilemapPos);
}
}
如何获取单元格的实际中点?请尝试tilemap.cellbunds.center
试试tilemap.cellBounds.center
好吧,我想我找到了问题所在 如果有人有同样的问题,下面是我如何解决的:
GetCellCenterWorld()
方法我在问题中发布的代码现在运行良好。好的,我想我找到了问题所在
[SerializeField] private Tilemap map;
private Camera camera;
[SerializeField] private Transform playerTransform;
void Start(){
camera = Camera.main;
}
// Update is called once per frame
void Update(){
if (Input.GetMouseButtonDown(0)){
var worldPos = camera.ScreenToWorldPoint(Input.mousePosition);
Vector3Int tilemapPos = map.WorldToCell(worldPos);
playerTransform.position = map.CellToWorld(tilemapPos);
Debug.Log(tilemapPos);
}
}
如果有人有同样的问题,下面是我如何解决的:
GetCellCenterWorld()
方法[SerializeField] private Tilemap map;
private Camera camera;
[SerializeField] private Transform playerTransform;
void Start(){
camera = Camera.main;
}
// Update is called once per frame
void Update(){
if (Input.GetMouseButtonDown(0)){
var worldPos = camera.ScreenToWorldPoint(Input.mousePosition);
Vector3Int tilemapPos = map.WorldToCell(worldPos);
playerTransform.position = map.CellToWorld(tilemapPos);
Debug.Log(tilemapPos);
}
}
playerTransform.position=map.CellToWorld(单击单元格位置)
只需将其转换回并提供tilemapPos
playerTransform.position=map.CellToWorld(单击单元格位置)
只需将其转换回并提供
tilemapPos
不幸的是,这给了我tilemap的中心而不是实际tile的中心:/不幸的是,这给了我tilemap的中心而不是实际tile的中心:/