Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


GetCellCenterLocal(Vector3Int)

Gets the logical center coordinate of a Grid cell in local space. The logical center for a cell of the Tilemap is defined by the Tile Anchor of the Tilemap.
Read time 1 minuteLast updated 6 days ago

Definition

public Vector3 GetCellCenterLocal(Vector3Int position)

Parameters

position

Grid cell position.

Returns

Type

Description

Vector3Returns the center of the cell transformed into local space coordinates.

Remarks

In a rectangular grid layout, a call to GridLayout.CellToLocal with Vector3Int parameter, returns a Vector3 coordinate that represents the bottom-left of the cell. While mathematically correct, you may prefer the center of the cell, for example when instantiating a GameObject into the grid.

Examples

// Snap the GameObject to parent Tilemap center of cellusing UnityEngine;using UnityEngine.Tilemaps;public class ExampleClass : MonoBehaviour{ void Start() { Tilemap tilemap = transform.parent.GetComponent<Tilemap>(); Vector3Int cellPosition = tilemap.LocalToCell(transform.localPosition); transform.localPosition = tilemap.GetCellCenterLocal(cellPosition); }}