Vector3Int SnapToGrid( int gridSize, bool sx, bool sy, bool sz )

book_4_sparkGenerated
code_blocksInput

Description

The SnapToGrid method in the Vector3Int struct is used to align the vector to a specified grid size. This method allows you to snap the vector's components to the nearest grid point based on the provided grid size and the specified axes (x, y, z) to snap.

Usage

To use the SnapToGrid method, you need to specify the grid size and which axes you want to snap. The method will return a new Vector3Int instance with the components snapped to the nearest grid points.

Parameters:

  • gridSize (System.Int32): The size of the grid to snap to. This value determines the spacing between grid points.
  • sx (System.Boolean): A boolean indicating whether to snap the x component.
  • sy (System.Boolean): A boolean indicating whether to snap the y component.
  • sz (System.Boolean): A boolean indicating whether to snap the z component.

Example

// Example of using SnapToGrid
Vector3Int position = new Vector3Int(13, 27, 42);
int gridSize = 10;

// Snap the position to the grid on all axes
Vector3Int snappedPosition = position.SnapToGrid(gridSize, true, true, true);

// snappedPosition will be (10, 30, 40) if gridSize is 10.