Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Tilemap.GetCellCenterLocal

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public method GetCellCenterLocal(position: Vector3Int): Vector3;
public Vector3 GetCellCenterLocal(Vector3Int position);

Parameters

position Grid cell position.

Returns

Vector3 Center of the cell transformed into local space coordinates.

Description

Get the logical center coordinate of a grid cell in local space.

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.

no example available in JavaScript
// Snap the GameObject to parent Tilemap center of cell
using 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); } }

Did you find this page useful? Please give it a rating: