- Home /
really annoying rotation problem.
i am working on a camera script and it is not going well. i want to make it so if my player is upside down that the camera rotates 180 degrees then stops. can someone help me find my mistake?
if (target.transform.localScale.y < 0){ upsideDown=true; }else{ upsideDown= false; } if (upsideDown == true){ if (r1 == true){ transform.Rotate(0,0,1); } if (transform.rotation.z >=180){ transform.rotation.z = 180; r1 = false; }else{ r1 = true; } }
if (upsideDown == false){ if (r2 == true){ transform.Rotate(0,0,-1); } if (transform.rotation.z <=0){ transform.rotation.z = 0; r2 = false; }else{ r2 = true; } }
Format this code by highlightiing it and using the 101010 button
You probably shouldn't rotate objects by inverting their scale, this can cause mesh normals to be placed inside-out as well as other problems. Here's a solution that works with rotation rather than scale
if (Vector3.Dot(transform.up, Vector3.down) > 0)
upsideDown = true;
else
upsideDown = false;
This script should rotate your camera up vector without changing the direction.
float maxDeg = 1f;
if (upsideDown )
{
Camera.main.transform.rotation = Quaternion.RotateTowards(Camera.main.transform.rotation, Quaternion.LookRotation(Camera.main.transform.forward, Vector3.down), maxDeg);
}
else
{
Camera.main.transform.rotation = Quaternion.RotateTowards(Camera.main.transform.rotation, Quaternion.LookRotation(Camera.main.transform.forward, Vector3.up), maxDeg);
}
Your answer
Follow this Question
Related Questions
Problem with Constraint Camera Rotation C# 3 Answers
Lerp Rotation broken 1 Answer
How does one use quaternion.euler(x,y,z) to rotate on y axis? 2 Answers
How do I set and get local rotation of objects 1 Answer
Rotating an already rotated object. 1 Answer