# Snap vector direction to nearest axis.

I have a case where I need to snap my character's direction to the nearest axis. It looks something like this.

My solution is to identify the axis with the largest absolute value, set it to 1, and set the other axes to 0. I also retain the sign of the largest axis.

Example, we have **Vector3(-0.5f, 0.1f, 0)**. The snapped direction is Vector3(-1,0,0).

```
Vector3 SnappedToNearestAxis(Vector3 direction){
float x = Abs(direction.x);
float y = Abs(direction.y);
float z = Abs(direction.z);
if (x > y && x > z){
return Vector3(Sign(direction.x),0,0);
} else if (y > x && y > z){
return Vector3(0,Sign(direction.y),0);
} else {
return Vector3(0,0,Sign(direction.z));
}
}
```