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 axis-specific snapping options.

Usage

To use the SnapToGrid method, you need to specify the grid size and which axes should be snapped. The method will return a new Vector3Int instance with the components adjusted 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): If true, the X component of the vector will be snapped to the grid.
  • sy (System.Boolean): If true, the Y component of the vector will be snapped to the grid.
  • sz (System.Boolean): If true, the Z component of the vector will be snapped to the grid.

Example

// Example usage of 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);

// Output: snappedPosition will be (10, 30, 40)