I have an object that I am rotating with swipe movement. I want to clamp rotation at 90 and -90 degrees, but the Quaternion.Euler doesn’t seem to be the correct values og I am missing som info on how the work.
void Update()
{
if(Input.GetMouseButton(0))
{
mPosDelta = Input.mousePosition - mPrevPos;
transform.Rotate(0.0f, -mPosDelta.x*speed, 0.0f);
}
//Clamp rotation
// create a new vector 3, "tempAngles" from the current transform.eulerAngles;
tempAngles = transform.eulerAngles;
// when the transform.eulerAngles.y is greater than 180...
while(tempAngles.y > 180)
{
//subtract 360 from transform.eulerAngles.y so it is definitely negative.
tempAngles.y -= 360;
}
//then create a new float variable for the y angle clamp using the tempAngles above.
float clampedY = Mathf.Clamp(tempAngles.y, -90, 90);
//then actually perform the new transform.
transform.eulerAngles = new Vector3(0f, clampedY, 0f);
mPrevPos = Input.mousePosition;
}