为什么团结匆忙显示错误?
IM使用免费资产模型创建了一个3D TILEMAP,如下所示:
,这是我试图获得其位置的代码:
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
//read in another post that changing to vector 2 will help
Vector2 mousePosition = (Vector2)Input.mousePosition;
Ray ray = cameraMain.ScreenPointToRay(mousePosition);
//performing raycast
if(Physics.Raycast(ray, out RaycastHit hitInfo))
{
//checking if hit the collider of gameobject and checking if the renderer is not null
if(hitInfo.collider.gameObject.GetComponent<Target>() != null)
{
Vector3 rayPosition = hitInfo.point;
//Vector3Int posInt = grid.WorldToCell(hitInfo.point);
Vector3Int posInt = grid.LocalToCell(rayPosition);
//Vector3Int posInt2 = tilemap.WorldToCell(mp);
//Shows the cell reference for the grid
Debug.Log(posInt);
// Shows the name of the tile at the specified coordinates
Debug.Log(tilemap.HasTile(posInt));
//Debug.Log(tilemap.GetTile(posInt).name);
}
}
}
}
我想使用gettile,但是它一直说对象引用未设置为对象的实例,因此我急忙检查瓷砖是否存在。由于某种原因,它显示为false。有人可以告诉我怎么了吗?
Im created a 3d tilemap using free asset models as shown below:
and here is the code where I am trying to get its position:
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
//read in another post that changing to vector 2 will help
Vector2 mousePosition = (Vector2)Input.mousePosition;
Ray ray = cameraMain.ScreenPointToRay(mousePosition);
//performing raycast
if(Physics.Raycast(ray, out RaycastHit hitInfo))
{
//checking if hit the collider of gameobject and checking if the renderer is not null
if(hitInfo.collider.gameObject.GetComponent<Target>() != null)
{
Vector3 rayPosition = hitInfo.point;
//Vector3Int posInt = grid.WorldToCell(hitInfo.point);
Vector3Int posInt = grid.LocalToCell(rayPosition);
//Vector3Int posInt2 = tilemap.WorldToCell(mp);
//Shows the cell reference for the grid
Debug.Log(posInt);
// Shows the name of the tile at the specified coordinates
Debug.Log(tilemap.HasTile(posInt));
//Debug.Log(tilemap.GetTile(posInt).name);
}
}
}
}
I want to use getTile, but it keeps saying object reference not set to the instance of the object, so I used hasTile to check if the tile is there. For some reason it is displaying false. Can someone tell me what is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论