Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


GetCellCenterWorld(Vector3Int)

Get the logical center coordinate of a grid cell in world space.
Read time 1 minuteLast updated 6 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.GridModule
public Vector3 GetCellCenterWorld(Vector3Int position)

Parameters

position

Grid cell position.

Returns

Type

Description

Vector3Center of the cell transformed into world space coordinates.

Remarks

In a rectangular grid layout, a call to GridLayout.CellToWorld with Vector3Int parameter, returns a Vector3 coordinate that represents the bottom-left of the cell. This is mathematically correct, but when for example instantiating a GameObject into the grid, you often prefer the center of the cell instead.

Examples

// Snap the GameObject to parent Grid center of cellusing UnityEngine;public class ExampleClass : MonoBehaviour{ void Start() { Grid grid = transform.parent.GetComponent<Grid>(); Vector3Int cellPosition = grid.WorldToCell(transform.position); transform.position = grid.GetCellCenterWorld(cellPosition); }}