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

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.

You are free to assign Transform.up with your DIR vector, does this produce what you wish?

2 Likes

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.

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.

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.

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.

That’s surely what you want: Unity - Scripting API: Quaternion.FromToRotation