Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is it possible to get -angles from Quanternion.Angle

Discussion in 'Scripting' started by Kuera, Jul 27, 2023.

  1. Kuera

    Kuera

    Joined:
    Apr 22, 2023
    Posts:
    14
    I've got a top down project with a character that has two modes of rotation. One if you're just using the ASWD keys (where it rotates toward whatever direction you're moving to) and another where if you hold down right mouse button your character keeps rotation toward the mouse cursor but can still move in any of the key press directions(aim while moving type of thing) I've set up code though the Quaternion.Angle to give me an angle difference if my character is not moving in the same direction as he is facing while look at the mouse. So if the mouse pointer is north and he's running north the angle is 0, if the pointer is north and he rights east it's 90 degrees. That all works fine but my problem comes in I get 90 degrees even if he runs west and the pointer is north. There is no difference in angle because I understand the degrees is 90 regardless. I would like to know if there's a way to change that as I would like to use the degree difference for several purposes, one been it would help with the animation controller to set which animation to use (such as stafing left or right while running forward or backward).
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    It's simply your code, you can change whatever you like. :)
     
  3. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    280
    There may be other ways to do it, but the way I always remember is that if you take the cross product of the players forward vector and the vector from the player to the mouse position, on the same plane in world space as the player, it can give you information about the angle. Cross products are not commutative, so the order of the vectors matters. The result is a new vector that represents the normal of the plane those two vectors reside on (scaled if your two initial vectors are not normalized), but it will point up or down depending on the angle between the vectors, and whether it's acute or obtuse. That's why cross product order is not commutative.
     
  4. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    280
  5. Kuera

    Kuera

    Joined:
    Apr 22, 2023
    Posts:
    14
  6. Kuera

    Kuera

    Joined:
    Apr 22, 2023
    Posts:
    14
    This has the same flavour as when someone asks "Can I do {Insert question}?" and the other party answers "I don't know, can you?" lol. I guess I'll find a method eventually. The Signed Angle option someone mentioned seems doable though.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Hmm, I swear I typed something else after that asking for more info; what the heck?!! I did have a lot of posts yesterday and I've got a full head-cold so I'll offer that as a reason. :confused:
     
  8. Kuera

    Kuera

    Joined:
    Apr 22, 2023
    Posts:
    14
    Haha it's all good. I managed to sort it out. I used a vector3.cross between the characters walking front vector and target. Used the - outcome to change the quaternion.angle output depending on what side I was on. If anyone is interested I'll post the code.
     
    MelvMay likes this.