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 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. You can choose which components (x, y, z) should be snapped by setting the corresponding boolean parameters.

Usage

To use the SnapToGrid method, call it on an instance of Vector3 and provide the grid size and boolean flags for each axis. The method will return a new Vector3 instance with the components snapped to the grid.

Parameters:

  • gridSize (System.Single): The size of the grid to snap to.
  • sx (System.Boolean): If true, the x component will be snapped to the grid.
  • sy (System.Boolean): If true, the y component will be snapped to the grid.
  • sz (System.Boolean): If true, the z component will be snapped to the grid.

Example

// Example of using SnapToGrid
Vector3 position = new Vector3(3.7f, 4.2f, 5.9f);
float gridSize = 1.0f;

// Snap all components to the grid
Vector3 snappedPosition = position.SnapToGrid(gridSize, true, true, true);

// Result: snappedPosition will be (4.0, 4.0, 6.0)