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 ($$anonymous$$ath.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 $$anonymous$$UCH YOU REALLY ARE A$$anonymous$$AZING, 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
Follow this Question
Related Questions
Question about NavMesh and Rigidbody 0 Answers
transform.Rotate having no effect 0 Answers
Question About NavMesh Agent and rotation 1 Answer
Rotate object with 90 degrees? 1 Answer
Why isn't my object lerping ? C# 2 Answers