Search Unity

Help Needed Learning Quaternions (Or at least how to use them)

Discussion in 'Scripting' started by orionburcham, Jul 25, 2011.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    I'm embarking on a journey to understand how quaternions work (at least well enough to use them for rotations in Unity), but so far I've been striking out looking for easily digestible information online. I think this may be because it's just not information that can be easily simplified, and maybe asking for a beginner's guide to Quaternions is just asking too much. But I've hit a wall and I'm looking for any advice.

    If anyone out there can offer help in understanding, or suggestions for learning sources, it'd be a huge help. For reference, here's the original problem I'm trying to solve:

    I need to rotate one point, about another point, by x degrees.

    Its basically the same as if I had a GameObject A parented to a GameObject B (which has a different position than A), and then I rotated GameObject B. In that case Unity will just do the rotations for you behind the scenes, but I'm in a situation where that won't work, and I need to know how to rotate the vector entirely with math.

    I've been attempting to learn this stuff for a long while now with little luck, and any help or leads would be greatly appreciated.

    Thank you,
    Orion
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    You could use transform.Rotate(); which takes a vector3 or even EulerAngles
     
  3. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Unfortunately, I don't think transform.Rotate() or transform.RotateAround() can work in my case, since I need to rotate a point represented by a Vector3 in my script (not a GO), so there won't be a transform involved.

    I don't mean to shrug off your help, though. Please let me know if I'm wrong, or if you know any other solutions.

    Thanks!
     
    Last edited: Jul 25, 2011
  4. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
  5. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    can I use either of these methods if I'm trying to rotate a virtual point (vector 3) in my script, and don't have a transform involved? I'm not meaning to rotate a GO.
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    From my experience there is no reason to learn about Quaternions unless you have an interest in them. You can do everything in Euler and just use the inbuilt functions to convert between.

    For what you're doing specifically, I generally just cheat. I change the objects pivot point to the point where I want to orbit around and just do a normal rotation. I really should get around to writing a solid orbit script sometime though.
     
  7. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    oops I did not see your reply to appels, or get it that your target is also only a coordinate value and not a GO.
    Those functions do make the calculations a lot easier ( or rather make it so you dont have to do the calculations).
    I would be inclined to create an empty gameobject on the fly, do the manipulation, get the coordinates, and then destroy it.
    That might also be a factor if you are finding the rotated value just once or doing it continuously.

    edited typo: I wrote "included" when I meant to write "inclined"
     
    Last edited: Jul 25, 2011
  8. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    ha that worked so well! Thank god I don't have to learn about Quaternions! :p (although I really probably should at some point, I suppose). Thanks a lot for the help, guys. You just saved me a lot of sweat and tears.