# Seemingly simple rotation problem

I’ve been pulling my hair over this for days. I just cannot seem to get it: Look at this picture:

All I want is user to choose:
X-Axis of cube should match the Z direction of the other transform.
Z-Axis of the cube should match the Y direction of the other transform.

I’ve tried many things. I can perform the transformations individually, but cannot figure out how to do them both sequentially, the rotation just screws up. Any help is appreciated.

Okay, so if I’m following this correctly, you want ObjectA’s X direction to be ObjectB’s Z direction, and ObjectA’s Z direction to be ObjectB’s Y direction, so if ObjectA were sitting exactly on the origin and at identity rotation, and ObjectB was in exactly the same location, ObjectA would be facing “forward”, and ObjectB would be facing “right” AND rotated onto its left side? That’s a 90 degree rotation to the right along the Y axis, 90 degree rotation to the left on the Z axis, so:

``````ObjectA.transform.rotation.SetLookRotation(ObjectB.transform.right, ObjectB.transform.forward);
``````

… or something?

Sorry I forgot to say:
X-Axis of cube should match the Z direction of the other transform.
Z-Axis of the cube should match the Y direction of the other transform.
and any other similar combinations:
i.e.
Z → Y, Y-> Z, Z → X, X → Z, X → Y, Y → X etc.

edit: The “Cube” can be in any orientation, the transform I’m trying to map the directions to is always constant.

If this isn’t a good way to go then how about changing the non-cube transforms directions to match what I wanted as Up/Forward from the user? Would that be easier?

They’re all easy. If you want to match the axis, use SetLookRotation to match it to whatever you want. The first parameter sets the “forward” direction, the second sets the “up” direction. Those are all you need to set to perfectly match up the axis for both objects.

If you want to be able to do it by passing in some sort of variable substitute, just use:

``````Vector3 vectorDirectionA = Vector3.forward;
Vector3 vectorDirectionB = Vector3.up;

ObjectA.transform.rotation.SetLookRotation(ObjectB.transform.TransformPoint(vectorDirectionA), ObjectB.transform.TransformPoint(vectorDirectionB));
``````

Where the A and B directions can be any normal direction shortcut. All axis will match up.

Doesn’t seem to work:

``````    Vector3 forward = Vector3.forward;
if(io.ForwardAxis == DirectionAxis.Green)
{
forward = Vector3.up;
}
else if(io.ForwardAxis == DirectionAxis.NegGreen)
{
forward = Vector3.down;
}
else if (io.ForwardAxis == DirectionAxis.Red)
{
forward = Vector3.right;
}
else if (io.ForwardAxis == DirectionAxis.NegRed)
{
forward = Vector3.left;
}
Vector3 up = Vector3.up;
if (io.UpAxis == DirectionAxis.Blue)
{
up = Vector3.forward;
}
else if (io.UpAxis == DirectionAxis.NegBlue)
{
up = Vector3.back;
}
else if (io.UpAxis == DirectionAxis.Red)
{
up = Vector3.right;
}
else if (io.UpAxis == DirectionAxis.NegRed)
{
up = Vector3.left;
}
transitionPoint.rotation.SetLookRotation(forward,up);
``````

transitionPoint rotation never changes!

You left out a BIG piece of the code I posted. Notice that I used ObjectB.transform.TransformPoint(direction) for both parameters? You need to replace the “direction” in there, not the whole thing. You can’t make a rotation relative to another rotation without actually referencing the second rotation in any way.

Edit: You should use TransformDirection instead of TransformPoint. Since you’re using shortcut directions, it won’t make any difference at all to the result, but it will be a lot faster.

I did actually notice and add that after, exactly the same result. All that code does is make my “cube(Object B)” transform.forward face toward the forward direction of ObjectA. This isn’t really what I wanted.

I’ll try explain it a different way:

Look at the transform in the original picture (The one not attached to the cube).
I want it’s current Y to instead be in it’s Z direction and it’s current X to be in the direction it’s Y used to be. And all possible combinations of that.

Sorry for such bad explanations! I’m sure that’s not helping with solving this myself ha

``````Transform aTransform = ObjectA.transform;
Transform bTransform = ObjectB.transform;

Vector3 bWorldDir1 = bTransform.position - bTransform.TransformDirection(vectorDirectionA);
Vector3 bWorldDir2 = bTransform.position - bTransform.TransformDirection(vectorDirectionB);

Vector3 aRelative1 = aTransform.InverseTransformDirection(aTransform.position + bWorldDir1);
Vector3 aRelative2 = aTransform.InverseTransformDirection(aTransform.position + bWorldDir2);

aTransform.rotation.SetLookRotation(aRelative1, aRelative2);
``````

Sorry, I blame the fact that I’m exhausted. Once again, ObjectA and ObjectB refer to your two GameObjects, and vectorDirectionA and B refer to those random directions that you choose with input commands.

Yup, I’m tired. I knew I was being an idiot the moment it turned into more than 2 lines to do this. I think the last one would work too, but it’s a really pointlessly roundabout way to do it. >_>;

``````ObjectA.transform.rotation = ObjectB.transform.rotation;
ObjectA.transform.rotation.SetLookRotation(vectorDirectionA, vectorDirectionB);
``````