Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

transform.rotate/rotation?

Discussion in 'Scripting' started by MikeC, Oct 14, 2006.

  1. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    Hi all!

    I've got my character, and a trigger. The trigger triggers an animation and sets the position before and after the animation. However, the triggered animation is rotation sensitive and I could really do with getting the trigger to alter the rotation of the character too.

    For the position I just used:

    Code (csharp):
    1. transform.position = Vector3(-113, 43, 462);
    So I figured something similar would apply:

    Code (csharp):
    1. transform.rotation = Vector3(-0, 150, -0);
    But it doesn't like it! I checked the docs and it's going on about eulers and quaternions and I just don't understand it this late on a saturday night ;)

    Can anyone help?

    Thanks!
    Mike
     
  2. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Maybe
    Code (csharp):
    1. transform.eulerAngles = Vector3(-0, 150, -0);
    ?
     
  3. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Quaternions are not an easy concept to understand. Basically you need to abstract yourself from them. You need to know that a Quaternion is another way to represent a rotation, but unlike euler angles which are in 3 dimensional space, Quaterions are represented on 4 dimensional space (so it's not possible to imagine a rotation on Quaternion - at least not for me!).

    You can see in the following link how a rotation in euler angles is transformed into a Quaternion (q0, q1, q2 and q3 formulas)
    http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles.

    If you had some math, you probably learned Complex Numbers, where square root of -1 is 'i'. On Quaternions you have the same, but with 'i', 'j', and 'k' (i.e. like an extension of Complex Space).

    Quaternions are important because they eliminate a rotation problem that euler angles have (if you rotate 90º around an axis, you loose a degree of freedom, so when you apply another rotation you will get some "odd" rotations).
     
  4. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    Thanks for the info, guys! I'll see what I can do.

    Mike
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Given how few people understand quaternions (I've always been good at math, and I'm still having a hard time wrapping my head around them), could there be some constants and/or more easily understood constructors for them?

    For example, a constructor that is passed a single Vector3 could make it face that vector and assume that up = up. This would make easy statements like facing = Quaternion(Vector3.left);

    In the meantime, could someone be so kind as to post the xyzw constructor values for some of the easy quaternions (like forward, backward, left, right)?
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Did you take a look at LookRotation in the Quaternion documentation?

    Your example above would then become
    Code (csharp):
    1. facing = Quaternion.LookRotation(Vector3.left);
     
  7. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Thanks, I missed those!

    Still... would have seen them if they were constructors. ;-)
     
  9. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    The problem with constructors vs. static functions is that you can not tell from the constructor name what it will do with the inputs. Should a constructor that takes two Vector3s as parameters treat them as a direction and a world-up vector like Quaternion.LookRotation does or should it treat it as a from and to vector pair like Quaternion.FromToRotation does? The same applies to a constructor that takes a single Vector3. Should it treat is as Quaternion.Euler does or should it do the same as the single-parameter version of Quaternion.LookRotation?