Vector3 SnapToGrid( float gridSize, bool sx, bool sy, bool sz )

book_4_sparkGenerated
code_blocksInput

Description

The SnapToGrid method in the Vector3 struct is used to adjust the components of a vector to align with a specified grid size. This method allows you to snap the vector's x, y, and z components to the nearest grid point, based on the provided grid size and boolean flags for each axis.

Usage

To use the SnapToGrid method, call it on an instance of Vector3 and provide the grid size and boolean flags indicating which axes should be snapped. The method returns a new Vector3 with the snapped values.

Parameters:

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

Example

// Example usage of SnapToGrid
Vector3 position = new Vector3(3.7f, 4.2f, 5.9f);
float gridSize = 1.0f;
Vector3 snappedPosition = position.SnapToGrid(gridSize, true, true, false);
// snappedPosition will be (4.0f, 4.0f, 5.9f)