Trouble Lerping a local Euler angles within some constraints (positive and negative)

Hi all, first time posting because I’m usually pretty confident in my scripting, but when it comes to rotations I’m a total noob!

At the moment, i’m trying to move an object on its local x-axis by ± 30 degrees.

I have two functions at the moment:

private void TurnBuckleOne() {
 var wantedVal = Mathf.Lerp(-30, 0, turnBuckle1);
        antenna.localEulerAngles = new Vector3(wantedVal, antenna.localEulerAngles.y, antenna.localEulerAngles.z);
}
private void TurnBuckleTwo() {
 var wantedVal = Mathf.Lerp(30, 0, turnBuckle2);
         antenna.localEulerAngles = new Vector3(wantedVal, antenna.localEulerAngles.y, antenna.localEulerAngles.z);
}

Where turnBuckle1 and turnBuckle2 are values from 0 → 1. These values are controlled individually by a slider/another function which calls either TurnBuckleOne or TurnBuckleTwo respectively everytime the percent changes. At the moment, both these values start at 1.

at the moment, these two work perfectly fine individually!

Turnbuckle one successfully moves the angle between 330 → 0
Turnbuckle two successfully moves the angle between 0 → 30

However, my problem comes when I want both of them to interact with each other. So for example, if I use TurnBuckleOne and its local euler axis goes to 350 for example, I then want to be able to use TurnBuckleTwo but can still only be within the range. My intention is that since the range for TurnbuckleTwo can only move up to +30 degrees, by changing the slider, the new effective localEuler angle range should be between 350 → 380 (20 when looping).

For context, I’ve tried the following, but can’t seem to quite get it. I’ve tried using mathf.LerpAngle but perhaps I’m using it wrong. But the second one that I was sure that would work is that everytime I switch between the functions, I set an “AnchorAngle” which sets where the current angle was. That way, when I switch to the other function it knows the range in which it can start. My best guess is that my Lerp is off/not doing what I want?

private void TurnBuckleOne() {
 if(index != 1)
        {
            anchorAngle = antenna.localEulerAngles;
            index = 1;
        }
 var wantedVal = Mathf.Lerp(anchorAngle.x-30, anchorAngle.x, turnBuckle1);
        antenna.localEulerAngles = new Vector3(wantedVal, antenna.localEulerAngles.y, antenna.localEulerAngles.z);
}
 if(index != 2)
        {
            anchorAngle = antenna.localEulerAngles;
            index = 2;
        }
private void TurnBuckleTwo() {
 var wantedVal = Mathf.Lerp(anchorAngle.x + 30, anchorAngle.x, turnBuckle2);
         antenna.localEulerAngles = new Vector3(wantedVal, antenna.localEulerAngles.y, antenna.localEulerAngles.z);
}

Happy to discuss because this is the longest I’ve been stuck on a problem in a while and I’m thinking it’s a simple fix that I’m hopefully just not understanding for some reason ahha, thanks for reading!

Manipulating or relying on .eulerAngles or .localEulerAngles is almost always a dead end.

Instead, what are you actually trying to do rotation-wise?

Here’s some more reading:

All about Euler angles and rotations, by StarManta:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

Hi @Kurt-Dekker Yeah, i’m going to try using Quaternions now to see if I can get the results I want.

I guess high level rotation wise, I want each slider to be able to rotate an object’s local x axis.

Slider 1 can rotate an object a range anywhere from -30 to 0 degrees.

Slider 2 can rotate an object a range anywhere from 0 to 30 degees.

Your best bet is to have local-only float variables to represent traverse and elevation, then change and clamp those floats, and finally drive .localRotation of the parts on their correct Transform axis.

Here’s an example in a turret aiming/rotating context:

More from our very own orionsyndrome:

I appreciate all the help @Kurt-Dekker !

I ended up finding a solution.

Ultimately, I needed to consider both float values simultaneously instead of individually.

For some reason I thought that I could treat them as separate entities when in reality I needed to combine the two values and have their summation of the two be the end result for the final local x vector.

At the end of the day, seemed to be more a misunderstanding of combining two results!

Cheers!