Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved Getting rotation in 360 degrees via threshold

Discussion in 'Scripting' started by ProgrammingWithEDM, Oct 31, 2023.

  1. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    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:

    https://gist.github.com/kurtdekker/0f5f353911d7a064e20868e4e5f4dba4
     
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    It happens I just wrote code yesterday that checks if an angle is close to another one (tolerance), I'll share it:

    Code (CSharp):
    1. public static bool IsCloseToAngle(this float angle, float targetAngle, float tolerance)
    2. {
    3.     // Edit: Thanks to Kurt-Dekker, all this code can be replace with just one line:
    4.  
    5.     // return Mathf.Abs(Mathf.DeltaAngle(angle, targetAngle)) <= tolerance;
    6.  
    7.     angle = (angle % 360.0f + 360.0f) % 360.0f;
    8.  
    9.     var angularDifference = Mathf.Abs(angle - targetAngle);
    10.  
    11.     return angularDifference <= tolerance || angularDifference >= 360 - tolerance;
    12. }
    Usage:

    Code (CSharp):
    1. var angle = 82f;
    2. var isCloseEnough = angle.IsCloseToAngle(90, 10);
    3. // returns true if the angle is 90 ± 10 degrees (80 - 100 range). In this case yes.
     
    Last edited: Oct 31, 2023
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Nad_B likes this.
  5. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    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?
     
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Kurt-Dekker likes this.
  7. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    When i use this do i replace angle with the rotation or Euler of my transform?
     
  8. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Euler.
     
  9. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    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
     
  10. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Well time to Debug as Kurt said. Debug.Log your Euler angle and the result of the function each frame and check its value.

    Code (CSharp):
    1. Debug.Log($"Current hand angle: {handAngle:N0}. Close enough to 180?: {(isCloseEnough ? "YES" : "NO")}");
     
    Last edited: Oct 31, 2023
  11. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    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
     
  12. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    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.
     
  13. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    as expected, your angle maxes at 90, have you tried using a angle higher than 90?
     
  14. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    I am only rotating it around the x axis right now
     
  15. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    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
     
  16. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    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.
     
  17. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    here is a image of it at values of 0, 90, and 180. I have the target at 180,
     

    Attached Files:

  18. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Something is wrong/overriding Unity Editor hand rotation. Show us the code that set the hands rotation? is it a plugin?
     
  19. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
  20. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    unity xr interaction toolkit we use the action based controllers
     
    Nad_B likes this.
  21. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    So there's probably another way to get the hands rotation except reading rotation from the game object (maybe it is creating a child game object that gets rotated?)
     
  22. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    i understand that, but i will say i am rotating the part my self in the inspector
     
  23. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Yes and maybe it gets overridden by the XR plugin... Anyway the function I gave you works, you just need to find a way to get the real and actual hand rotation, this should be your real question. Good luck!
     
  24. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    i tried it on i different completely new object and got the same issue
     
  25. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
  26. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,164
    When comparing directions it's nearly always best to use the dot product of vectors.

    Code (CSharp):
    1.       if (Vector3.Dot(handDirection,player.transform.forward)>0.9f)
    2.          Debug.Log("The hand is pointing roughly in the same direction the player is looking");
     
  27. ProgrammingWithEDM

    ProgrammingWithEDM

    Joined:
    Sep 2, 2021
    Posts:
    19
    I would like to thank you all for your help, Zulo3ds worked well to achieve what I was going for. Thanks!