# how to snap rotate my cube to the nearest 90 degree when i let got of my thumb stick ?

```
public class CubeRotation : MonoBehaviour
{
public JoyStick virtualJoystick;
public float RoatitionSpeed = 20f;
private static int SnapRot;
private float LeftoOrRight;
private float UpOrDown;
private Vector3 CurrentRotation;
private void Start()
{
}
private void Update()
{
CurrentRotation.z = 0;
UpOrDown = virtualJoystick.Vertical();
LeftoOrRight = virtualJoystick.Horizontal();
// thunbstick rotation....
if (LeftoOrRight > 0.93f)
{
transform.Rotate(Vector3.up * -RoatitionSpeed * Time.deltaTime, Space.World);
}
else if(LeftoOrRight < -0.93f)
{
transform.Rotate(Vector3.up * RoatitionSpeed * Time.deltaTime, Space.World);
}
if(UpOrDown > 0.93f)
{
transform.Rotate(Vector3.right * RoatitionSpeed * Time.deltaTime, Space.World);
}
else if (UpOrDown < -0.93f)
{
transform.Rotate(Vector3.right * -RoatitionSpeed * Time.deltaTime, Space.World);
}
// snap rotation
SnapRot = virtualJoystick.Snaprot;
CurrentRotation = transform.rotation.eulerAngles;
if (SnapRot == 1)
{
if(CurrentRotation.x >= 0.1f && CurrentRotation.x <= 45 )
{
transform.eulerAngles = new Vector3(0,0,0);
}
else if(CurrentRotation.x >= 45.1f && CurrentRotation.x <= 90)
{
transform.eulerAngles = new Vector3(90, 0, 0);
}
else if(CurrentRotation.x >= 90.1f && CurrentRotation.x <= 135)
{
transform.eulerAngles = new Vector3(90, 0, 0);
}
}
}
}
```

At the moment the rotation is nice but id like the cube to snap back in to place (to the nearest 90 degrees) when i let go of the thumb stick, i tried with it the, If eulerAngles are less than or grater than but it got confused after a while i'm learning ..... and i feel this way eventually the number work against each other in the end because i want it spinning on two Axis really..

**Answer** by gfoot
·
Apr 12 at 11:55 PM

The simplest way to do this is to take the cube's forward and up vectors, align them to the nearest world axes, and then recalculate the cube orientation using Quaternion.LookRotation.

To snap a vector along the nearest world axis, find out which of its components (x, y, or z) has the greatest magnitude (positive or negative), and zero out the other two. You don't need to normalize the result, LookRotation will handle it fine.

Something like this ought to work:

```
private static Vector3 NearestWorldAxis(Vector3 v)
{
if (Mathf.Abs(v.x) < Mathf.Abs(v.y))
{
v.x = 0;
if (Mathf.Abs(v.y) < Mathf.Abs(v.z))
v.y = 0;
else
v.z = 0;
}
else
{
v.y = 0;
if (Mathf.Abs(v.x) < Mathf.Abs(v.z))
v.x = 0;
else
v.z = 0;
}
return v;
}
...
Vector3 alignedForward = NearestWorldAxis(transform.forward);
Vector3 alignedUp = NearestWorldAxis(transform.up);
transform.rotation = Quaternion.LookRotation(alignedForward, alignedUp);
```

thank you so much here is how it looked in the end and worked like charm :D...... thank you again it was a big help. was wondering tho, could when the snap rotation start it be done over time.deltatime for a smoother,.. set in to place . like by half the rotation speed ?

```
`public class CubeRotation : MonoBehaviour
{
public JoyStick virtualJoystick;
public float RoatitionSpeed = 20f;
private static int SnapRot;
private float LeftoOrRight;
private float UpOrDown;
private static Vector3 NearestWorldAxis(Vector3 v)
{
if (Mathf.Abs(v.x) < Mathf.Abs(v.y))
{
v.x = 0;
if (Mathf.Abs(v.y) < Mathf.Abs(v.z))
v.y = 0;
else
v.z = 0;
}
else
{
v.y = 0;
if (Mathf.Abs(v.x) < Mathf.Abs(v.z))
v.x = 0;
else
v.z = 0;
}
return v;
}
private void Update()
{
UpOrDown = virtualJoystick.Vertical();
LeftoOrRight = virtualJoystick.Horizontal();
// thunbstick rotation....
if (LeftoOrRight > 0.93f)
{
transform.Rotate(Vector3.up * -RoatitionSpeed * Time.deltaTime, Space.World);
}
else if(LeftoOrRight < -0.93f)
{
transform.Rotate(Vector3.up * RoatitionSpeed * Time.deltaTime, Space.World);
}
if(UpOrDown > 0.93f)
{
transform.Rotate(Vector3.right * RoatitionSpeed * Time.deltaTime, Space.World);
}
else if (UpOrDown < -0.93f)
{
transform.Rotate(Vector3.right * -RoatitionSpeed * Time.deltaTime, Space.World);
}
// snap rotation
SnapRot = virtualJoystick.Snaprot;
if (SnapRot == 1)
{
Vector3 alignedForward = NearestWorldAxis(transform.forward);
Vector3 alignedUp = NearestWorldAxis(transform.up);
transform.rotation = Quaternion.LookRotation(alignedForward, alignedUp);
}
}
public void res()
{
gameObject.transform.rotation = Quaternion.identity;
}`
```

### 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.

### Follow this Question

### Related Questions

How to Set a Single Axis Rotation of a GameObject? 9 Answers

Parenting an object causes it to change rotation 1 Answer

UI compass pointing to a 3D object 0 Answers

How do i fix this default rotation? 0 Answers

Sprites only rotate by 90 degrees 0 Answers