WorldToCell(Vector3)
Converts a world position to cell position.
Read time 1 minuteLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.GridModule
public Vector3Int WorldToCell(Vector3 worldPosition)
Parameters
World Position to convert.
Returns
Type | Description |
|---|---|
| Vector3Int | Cell position of the world position. |
Remarks
A GridLayout has no bounds. Cells extend infinitely in every direction from the origin, so any world position maps to the cell that contains it.
Examples
// Snap the GameObject to parent GridLayoutusing UnityEngine;public class ExampleClass : MonoBehaviour{ void Start() { GridLayout gridLayout = transform.parent.GetComponentInParent<GridLayout>(); Vector3Int cellPosition = gridLayout.WorldToCell(transform.position); transform.position = gridLayout.CellToWorld(cellPosition); }}