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 is useful for snapping a vector to a grid, which is often needed in scenarios like game development or 3D modeling where objects need to be aligned to a grid for consistency or aesthetic purposes.
Usage
To use the SnapToGrid
method, you need to specify the grid size and which components (x, y, z) should be snapped. The method returns a new Vector3
instance with the specified components snapped to the nearest grid point.
Parameters:
gridSize
(System.Single): 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 of using SnapToGrid
Vector3 position = new Vector3(3.7f, 4.2f, 5.9f);
float gridSize = 1.0f;
Vector3 snappedPosition = position.SnapToGrid(gridSize, true, true, true);
// snappedPosition will be (4.0f, 4.0f, 6.0f)