So I'm trying to rotate an object from -35 degrees to 35 degrees, or rather 325 degrees to 35 degrees
So I'm trying to rotate an object from -35 degrees and 35 degrees, or rather 325 degrees to 35 degrees and I'm trying to lock the rotations there.
I have this code:
void lockRotationUp() {
Rotation.x = transform.rotation.eulerAngles.x;
Rotation.x = Mathf.Clamp (Rotation.x, 325,35);
transform.localEulerAngles = new Vector3(Rotation.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
}
but it just flips between 325 and 35 degrees, no in-the-middle, but if I write it this way:
void lockRotationUp() {
Rotation.x = transform.rotation.eulerAngles.x;
Rotation.x = Mathf.Clamp (Rotation.x, -35,35);
transform.localEulerAngles = new Vector3(Rotation.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
}
it freaks out when it hits zero, does anyone know how to properly lock the rotation from 325 degrees to 35 degrees?
Answer by mathacka · Nov 18, 2017 at 02:42 AM
I fixed one aspect of it, the locking aspect, but occasionally it flips back to the other side, here's the next iteration of the code:
void lockRotationUpDown() {
Rotation.x = transform.rotation.eulerAngles.x;
if (Rotation.x < minBound)
{
Rotation.x += 360.0f;
}
Rotation.x = Mathf.Clamp(Rotation.x, 360.0f-minBound, 360.0f+minBound);
if (Rotation.x >= 360.0f)
{
Rotation.x -= 360.0f;
}
transform.localEulerAngles = new Vector3(Rotation.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
}
it works when the sensitivity of rotation is adjusted to be a low number, ie.
transform.Rotate(0,0,Time.deltaTime * sensitivity);
unfortunately a low number is too low.
Ultimately I realized I've been doing this without the use of Quaternions, so I'm going to redo the entire rotation setup to use these and their functions.