Search Unity

How to align an objet's rotation with a direction's one axis?

Discussion in 'Scripting' started by Compressed, May 31, 2016.

  1. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    Hello,

    I have one game object in the scene. I also have some arbitrary direction, let's call it DIR. I need to rotate this game object so that its Y (up) axis is pointing in the same direction as the Y axis from DIR. But, very importantly, I need the game object to keep the direction of its remaining two axes unchanged (relative to the rotation). Please see the picture for more clarity.
    Basically what I want to achieve is to take the object's Y axis and push it it, so that is is aligned with the DIR's Y axis.
    I'm doing this in C#.

    Thank you.
     

    Attached Files:

    • quat.jpg
      quat.jpg
      File size:
      27 KB
      Views:
      1,581
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You are free to assign Transform.up with your DIR vector, does this produce what you wish?
     
    wickedwaring and ArachnidAnimal like this.
  3. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    Unfortunately no, if i did that, then the object's direction would become identical to DIR, and I don't want that, I only want the object's Y axis to point in the same direction as DIR's Y axis, as i drawn in the picture.
     
    Last edited: May 31, 2016
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I'm confused, what is Dir, is it a direction or an orientation? What your image describes are three sets of orthogonal vectors. If you want your transform to have the same up vector as described by another vector its as simple as assigning it and unity will rotate for you. If not you can try rotating with Quaternion.FromToRotation and feed it your original up vector and the desired one.
     
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    You can't rotate one axis and keeps the other two unchanged, they have to be orthogonal to each other. Unless you scale it negatively (flip it). But that's not a rotation.
     
    Last edited: Jun 1, 2016
  6. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    I don't want them to be aligned, basically, I want to take one direction axis and make the smallest rotation possible so that it matches the goal rotation axis.
     
  7. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353