Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取网格的中心&x27;s细胞统一_C#_Unity3d_Game Development - Fatal编程技术网

C# 获取网格的中心&x27;s细胞统一

C# 获取网格的中心&x27;s细胞统一,c#,unity3d,game-development,C#,Unity3d,Game Development,我正在和Unity做一个等距游戏,我需要知道细胞在世界坐标中的中心。我正在使用tilemap的GetCellCenterWorld()方法,但它返回了糟糕的结果。 我所期望的是单元的中心(基本上是逻辑中心),但Unity所返回的是什么(单元的顶部顶点?)。我相信这与精灵的轴心点(现在设置为其顶部中心)或tilemap属性中的单元锚点(我将其设置为(1,1))有关 下面是我用来“选择”瓷砖的代码: 公共类GridSelection:MonoBehavior { 公共Tilemap Tilemap;

我正在和Unity做一个等距游戏,我需要知道细胞在世界坐标中的中心。我正在使用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
好吧,我想我找到了问题所在

如果有人有同样的问题,下面是我如何解决的:

  • 将精灵轴心点设置为您希望单元中间的位置(在我的例子中,是草地部分的中心)
  • 将瓷砖锚定设置回(0.5,0.5)
  • 正常使用
    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);
        }
    }
    
    如果有人有同样的问题,下面是我如何解决的:

  • 将精灵轴心点设置为您希望单元中间的位置(在我的例子中,是草地部分的中心)
  • 将瓷砖锚定设置回(0.5,0.5)
  • 正常使用
    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的中心:/