Trying to MathF.Clamp y axis rotation results in x equal 270 and y equal max value instantly
I am in the process of prototyping several Amusement Park style rides for an upcoming relaxation program. One of the rides is a rotating airplane ride where the individual arms will go up and down with a maximum rotational value of, say, zero to 45 degrees.
This is what I'm using for my rotational scripts:
void Update () {
if(rotateMe) {
this.gameObject.transform.Rotate((X_RotateSpeed * Time.deltaTime), (Y_RotateSpeed * Time.deltaTime), (Z_RotateSpeed * Time.deltaTime), Space.Self);
tempAngle = transform.rotation.eulerAngles;
if (limitX)
{
angle = tempAngle.x;
angle = ClampAngle(angle, lowLimitX, highLimitX);
tempAngle.x = angle;
transform.rotation = Quaternion.Euler(tempAngle);
}
if (limitY)
{
angle = tempAngle.y;
angle = ClampAngle(angle, lowLimitY, highLimitY);
tempAngle.y = angle;
transform.rotation = Quaternion.Euler(tempAngle);
}
if (limitZ)
{
angle = tempAngle.z;
angle = ClampAngle(angle, lowLimitZ, highLimitZ);
tempAngle.z = angle;
transform.rotation = Quaternion.Euler(tempAngle);
}
} // end if rotateMe
} // end Update
private float ClampAngle(float cAngle, float min, float max) {
if (cAngle < -360.0)
cAngle += 360;
else if (cAngle > 360.0)
cAngle -= 360;
cAngle = Mathf.Clamp (cAngle, min, max);
if (min == max)
return cAngle;
if (cAngle == min || cAngle == max)
{
atLimit = true;
}
else
atLimit = false;
return cAngle;
}
The central column only has a Z rotation speed, and with no limits selected it rotates as expected on the Z axis.
If I set an arm to have a Y rotation speed, the arm slowly lifts an continues to rotate a full 360. The arm does not dislocate, maintaining position as it rotates on the 360.
However, we don't want to dump out the passenger. So I tried to set up a clamp with a zero-45 limitation and then re-assigning the value back to the rotation. In the Screen Shots, the blue plane is the one with the assignments.
Setting LimitY to true, as soon as I start the simulation in the Editor, my x-value is automatically set to 270 my y-value is set to 45 (or whatever the maximum is) IF I assign a low value to the rotation speed (like 5) but if I set a very high rotation speed (like 50), I observe my z-value set to 270, my y-value set to the max y and my x-value will increment until it hits 90. In both cases, once it hit's the 'assigned' x value (either 270 or 90) the airplane will no longer move up or down, but maintain it's facing and slowly move in a circle, with the arm acting like a piston on a train. Also, the actual Y-rotation does not appear to take place. The Arm itself seems to have rotated not quite 90 degrees clockwise from initial position of the arm, and then will rotate to keep the blue plane as close to that point as possible.
I have to assume the fault is within my ClampAngle function, but I've used the code I found in another forum to address having an object rotate with limitations on a specific axis, so I'm not sure what I've entered in incorrectly.
Any advice would be appreciated.
Joseph.
Answer by SeriousTangents · Jan 07, 2016 at 05:10 PM
I was wrong about the fault being in the ClampAngle function. It was caused by -where- I was pulling the EulerAngles from.
There is an option to select gameObject.Transform.localEulerAngles and, once I pulled from there, my rotations were spot on.
There was an issue when using negative rotations starting from zero, or when a negative rotation moved an object to zero. It would bounce back to the 45 degree point, and I realized what was happening there is when the rotational reaches zero, it switches to 360 and therefore is clamped above the max to the 45 degree max. This was fixed by setting the minimum to 1 (on a backward rotation) and 359 (on a forward rotation) and making sure the arms/wrists had a starting value of 1 and 359 respectively.
if (limitY)
{
tempAngle = this.gameObject.transform.localEulerAngles;
angle = tempAngle.y;
angle = ClampAngle(angle, lowLimitY, highLimitY);
tempAngle.y = angle;
transform.localEulerAngles = tempAngle;
}
Your answer
Follow this Question
Related Questions
Quaternion/World Local rotation problem 0 Answers
Strange 2D Physics Behavior 0 Answers
How to test for rotation? 0 Answers
Assign an object rotation along a single axis from other object's rotation. 0 Answers
Keydoor problem 0 Answers