# Snap direction to angles + shifting

I currently have this following code which snaps a v3 direction to the specified angles

```
Vector3 SnapToAngle(Vector3 vector3, float snapAngle, float snapAngleShift) {
float angle = Vector3.Angle (vector3, Vector3.up);
if (angle < snapAngle / 2) // Cannot do cross product
return Vector3.up * vector3.magnitude; // with angles 0 & 180
if (angle > 180 - snapAngle / 2)
return Vector3.down * vector3.magnitude;
float t = Mathf.Round (angle / snapAngle);
float deltaAngle = (t * snapAngle) - angle;
Vector3 axis = Vector3.Cross(Vector3.up, vector3);
Quaternion q = Quaternion.AngleAxis (deltaAngle, axis);
return q * vector3;
}
```

This was provided by @robertbu in this post. https://answers.unity.com/questions/493006/snap-a-direction-vector.html

Now. What I want to do, is to be able to shift the snap angle in such a way that if I set it to snap on 180 degrees increments, I can rotate the axis in which it snaps. Take a look at the image below.

any suggestions on how I can proceed?

Sorry but it's not quite clear around which axis you actually want to rotate. The original question where the code came from wanted to snap the rotation around the local x axis in 3d space. You drawn a pure 2d diagram with no references. The code of robert allows free rotation around the y axis but snaps the rotation around the local x axis.

Generally to apply a shift you just want to apply your shift rotation before any of the original code and undo that rotation before you return the result. However since the axis of rotation is not fixed in this code we can not apply a relative rotation beforehand since we don't know the axis yet. So if you want for example a 10° shift but the vector point exactly up it's impossible to determine a rotation axis for your shift. In his code he has two special cases for up and down. Though this only works because up and down are actual target snap angles.

Sorry for not being clear enough. The game I'm making is a 2d side scroller platformer that involves shooting. Therefore the rotation and snapping involve the Z axis. I've tried several approaches but I'm not really the best at math. Am I making my self clear?

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.