Description
The SnapToGrid
method in the Vector2
struct is used to adjust the vector's components to align with a specified grid. This is useful for snapping positions to a grid in 2D space, which can be particularly helpful in game development for aligning objects or ensuring consistent placement.
Usage
To use the SnapToGrid
method, call it on an instance of Vector2
. Provide the grid size and specify whether to snap each component (x and y) individually.
gridSize
: A float
representing the size of the grid cells.
sx
: A bool
indicating whether to snap the x component.
sy
: A bool
indicating whether to snap the y component.
Example
// Example of using SnapToGrid
Vector2 position = new Vector2(3.7f, 5.2f);
float gridSize = 1.0f;
Vector2 snappedPosition = position.SnapToGrid(gridSize, true, true);
// snappedPosition will be (4.0, 5.0) if both components are snapped to the nearest grid point.