为什么团结匆忙显示错误?

发布于 2025-02-06 11:08:13 字数 1659 浏览 2 评论 0原文

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:

enter image description here

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);
                
            }
       }
   }
}

and this is the result:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文