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

Modify quaternians

Discussion in 'Scripting' started by SchmermundP, Apr 6, 2014.

  1. SchmermundP

    SchmermundP

    Joined:
    Mar 20, 2014
    Posts:
    2
    Hello,

    I have ran into a problem with quaternions. My math is a little weak in this area, so some help would be quite appreciated. I have a transform that I want to set the Y value of the rotation to 0, so that its transform.forward will not contain any vertical components. however, transform.rotation takes a quaternion and from what I have read so far, there is no effective way to turn a Vector3 into a quaternion. Any advice would be greatly appreciated.

    Thanks in advance
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,414
  3. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    If you're going to be doing things like this, you should put some time into learning the math or you're going to have a really, really bad time. I don't mean the internals of Quaternions, but you should at least understand Euler angles.

    Off the bat, the way you're trying to do this won't work because:
    • the y value of an Euler angle does not correspond to pitch, it kinda-sorta-corresponds to yaw. x kinda-sorta-corresponds to pitch which is probably what you mean by vertical.
    • the x rotation of an Euler angle is applied after the z and before the y. which means you can't just set x to zero to "cancel the vertical".
     
    Last edited: Apr 6, 2014
  4. bigdaddy

    bigdaddy

    Joined:
    May 24, 2011
    Posts:
    153
    Look at Transform.localEulerAngles

    Code (csharp):
    1.  
    2. Vector3 localEA = transform.localEulerAngles;
    3. transform.localEulerAngles = new Vector3(localEA.x, 0f, localEA.z);
    4.