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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to set object rotation from 2 vectors

Discussion in 'Scripting' started by JonnyHilly, Jan 24, 2013.

  1. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    I have 2 vectors, defining an object's orientation... UP and Right
    but there seems to be no way to set an object's orientation using vectors.

    In Unity you can set the object's transform->forward OR Right OR up, to orient an object, and all of these are ambiguous, you cannot control the rotation around that axis (using the second vector) unity decides for you what angle that will be.

    isn;t there a way to construct a transform from 2 vectors ? or to set object's rotation ?

    There must be a simple way to do this please
    help appreciated, thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Lorrak likes this.
  3. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Quaternion.LookRotation allows you to create a rotation from two vectors:
    http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html

    This function uses for forward and up vectors, but you can easily get your forward vector by calculating the cross-product from your right and up vectors.
     
    abdulraheem_taha likes this.
  4. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    awesome, thats what I was looking for, thanks much !!!
    yep I actually have all 3 vectors already, calculated using .cross, just couldn't spot a way to do the unique orientation.
    cheers
     
  5. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    Hmm ok, I thought it was awesome until I tried it out....
    ok so if I individually just set transform.forward = myForward
    OR transform.right=myRight
    OR transform.up=myUp
    ...then it orients my object's chosen vector perfectly to the moving surface I'm trying to attach it to... confirming that I have correctly calculated vectors that describe the surface and orientation that I want. for up/right/forwards.
    However my object is always facing in the wrong direction (as I only provided 1 vector, which isn;t enough information to fully orient an object) no surprises so far. this is as expected.

    However...
    if I set the position using 2 vectors, transform.rotation.setLookRotation(myForward, myUp);
    (which should then be enough information to orient perfectly)
    what I end up is with none of the axis looking right,

    also I added rendered gizmos, so I can see my up/at/right and they are perfectly oriented and attached to the moving surface as intended....
    but using transform.rotation.setLookRotation(myForward, myUp);
    ....does not put the object at the same orientation !!! is it broken/bugged ?

    any other ideas ?
     
    Last edited: Jan 25, 2013
  6. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    ok found away to do it...

    I set the forward to my calculated forward.
    Then get angle difference, between the current right vector,and myRight... then rotateAround the new myForward, so current vector matches myRight.

    Works perfect.
    I'm pretty sure SetLookRotation must be bugged. or at least needs a more comprehensive description in the docs.