Getting rotation in 360 degrees via threshold

I am trying to get the rotation of my characters hands and see if each value separately is within a threshold. basically, I want to see if the hand is at 180 degrees with a threshold of 20 degrees otherwise saying I want to see if its x value is within 200 degrees and 160 degrees, and mi doing the same with the y and z but with other values. I have tried doing it with Euler angles and pretty much everything else. I’m very lost and am not the bet with rotation so if anyone knows how I would do this it would be very helpful.

This is for a hand sign magic kind of like Naruto if that helps.

Rotations are difficult to use for this.

I prefer using components one of the local vectors.

For instance if your finger was a transform pointing +Z, then consider transform.forward.y:

  • if your finger is flat, y will be zero or near zero

  • if you finger is up at all, y will be positive

  • if you finger is down at all, y will be negative

For instance, this was my head gesture recognizer:

The code:

It happens I just wrote code yesterday that checks if an angle is close to another one (tolerance), I’ll share it:

public static bool IsCloseToAngle(this float angle, float targetAngle, float tolerance)
{
    // Edit: Thanks to Kurt-Dekker, all this code can be replace with just one line:

    // return Mathf.Abs(Mathf.DeltaAngle(angle, targetAngle)) <= tolerance;

    angle = (angle % 360.0f + 360.0f) % 360.0f;

    var angularDifference = Mathf.Abs(angle - targetAngle);

    return angularDifference <= tolerance || angularDifference >= 360 - tolerance;
}

Usage:

var angle = 82f;
var isCloseEnough = angle.IsCloseToAngle(90, 10);
// returns true if the angle is 90 ± 10 degrees (80 - 100 range). In this case yes.

Are you aware of …

https://docs.unity3d.com/ScriptReference/Mathf.DeltaAngle.html

So if i was to set forwardY to z or x would it control the z or x rotation, or what axis controls which rotational axis?

No and I thank you for this! Unity is truly magnificent.

Need to ditch my old XNA habits of writing code before checking if there’s a readily available Engine solution.

When i use this do i replace angle with the rotation or Euler of my transform?

Euler.

so i have tried it with Euler and the other and it has not worked, i am setting the target angle to 180 and tolerance to 20, this has just always come back false

Well time to Debug as Kurt said. Debug.Log your Euler angle and the result of the function each frame and check its value.

Debug.Log($"Current hand angle: {handAngle:N0}. Close enough to 180?: {(isCloseEnough ? "YES" : "NO")}");

yes, i have done this and as it hits 90 degrees it always inverts and starts decreasing. I know this is because Euler changes the other variables and thats why im trying to find a way to get the rotation as 0-360, not 0 to 90 or 180

We’re talking here about one axis right? (your “hand” rotates around one axis only?) My function takes care of the range 0-360, so it’s not the problem.

as expected, your angle maxes at 90, have you tried using a angle higher than 90?

I am only rotating it around the x axis right now

What i am trying to do is test when the hands are at a certain position(Done)
doing a certain pose(Done)
and rotated in the right angle(Having issues)
this is a vr game and I’m trying to add hand signs as the way you do magic

The function I gave you works. As I said, do the logging I suggested and check the values printed in the console vs the one in the Editor and see if you’re passing the correct angle. If your Euler angle does not exceed 90 as you said, there’s something wrong in the way you calculates/assign the hand rotation itself, as it should continue increasing to 360 and go back to 0. Show us the code you use to assign the hand rotation.

here is a image of it at values of 0, 90, and 180. I have the target at 180,



Something is wrong/overriding Unity Editor hand rotation. Show us the code that set the hands rotation? is it a plugin?

Also check this

unity xr interaction toolkit we use the action based controllers