# unity 2d rotation flipping on y axis upside-down problems

favorite OK so this has been an issue for a week now i cant seem to figure it out i am trying to flip my submarine on its y axis once its goes upside-down. it rotates on my mouse position i will show you my code.

//This code rotates my submarine on mouse position which is perfect.

```
Vector3 RayPos = cam.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - RayPos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
rotateSpeed * Time.deltaTime);
```

//now this code is my problem once my sub goes upside down it flips (y) but it only does it once but then when i go back the correct way it wont flip on (y) anymore.

```
Debug.Log(Mathf.Abs(Vector3.Dot(transform.up, Vector3.up)));
if (Mathf.Abs(Vector3.Dot(transform.up, Vector3.down)) < 0.125f)
{
if (Mathf.Abs(Vector3.Dot(transform.right, Vector3.down)) > 0.825f)
{
SubTrans.localScale = new Vector3(transform.localScale.x, -1,
transform.localScale.z);
}
else
{
SubTrans.localScale = new Vector3(transform.localScale.x, 1,
transform.localScale.z);
}
}
```

Because when it's localscale is -1 the transform.up is flipped

```
Debug.DrawRay(transform.position, -transform.up, Color.green);
```

Something like this should solve it (NOT TESTED):

```
if (Math.Abs(transform.localScale.y - (-1)) < 0.01f)
{
if (!(Vector3.Dot(transform.up, Vector3.down) < 0.001f)) return;
if (Vector3.Dot(transform.right, Vector3.down) > 0.825f)
{
transform.localScale = new Vector3(transform.localScale.x, 1, transform.localScale.z);
}
else if (Vector3.Dot(-transform.right, Vector3.down) > 0.05f)
{
transform.localScale = new Vector3(transform.localScale.x, 1, transform.localScale.z);
}
}
else
{
if (!(Vector3.Dot(-transform.up, Vector3.down) < 0.001f)) return;
if (Vector3.Dot(-transform.right, Vector3.down) > 0.825f)
{
transform.localScale = new Vector3(transform.localScale.x, -1, transform.localScale.z);
}
else if (Vector3.Dot(transform.right, Vector3.down) > 0.05f)
{
transform.localScale = new Vector3(transform.localScale.x, -1, transform.localScale.z);
}
}
```

However it will cause a jitter in a certain mouse position because of this -> `0.001f)) return;`

The higher number will cause more jitter and lower will cause less...

THAN$$anonymous$$S SO MUCH YOU REALLY ARE AMAZING, that worked more than perfectly seriously thank you i asked this question 4 times now and finally someone can help me thanks alot

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