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

Setting Rotation from matrix

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

  1. Gaski

    Gaski

    Joined:
    Jul 25, 2011
    Posts:
    84
    Hi,

    I need to set the rotation and position of an object using a rotation matrix. From the matrix the up vector, the right vector and the forward vector that I desire. What is the best way to turn this into information (a Quaternion I assume) I can use on a unity game object.

    Thanks in advance
     
  2. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    You can use Quaternion.Lookrotation to create a rotation from a forward and up vector (the right vector follows automatically).
    http://unity3d.com/support/documentation/ScriptReference/Quaternion.LookRotation.html

    You cannot set the transformation matrices directly in a transform (their values are read-only), but the LookRotation function should suffice for almost all purposes. If you really need to set the matrices (because you need shearing, for example), you're only option is to pass your matrix to a shader with a custom vertex shader.
     
  3. Gaski

    Gaski

    Joined:
    Jul 25, 2011
    Posts:
    84
    Brilliant that's what In was looking for. Not sure how I missed over that function in the docs.

    Thanks for the help Tom