- Home /

# Trying to tween the rotation of a cube in x and z and having trouble

I'm trying to make a first person game where, on a very basic level, if the player looks at a cube and hits a button, the cube rotates away from the player 90 degrees. The cube and the player are sitting on a floor, so the y axis isnt a factor, just x and z. But it seems like no matter what methods I try, the best case scenario is that it works, but it gets really strange when you rotate one axis and then the other, or rotate the x axis beyond 360 degrees. Is there a way to fix this? I've tinkered with Quaternions and I have a feeling that's probably the answer, but I'm not super familiar with them, so that method hasnt been going well either.

Any help would be greatly appreciated.

-Taylor Anderson

### People who like this

**Answer** by TaylorAnderson
·
Sep 17, 2017 at 11:50 PM

Okay, I've solved this. For whoever reads this thread in the future, you may find this helpful.

Code:

```
MidiOut.SetInstrument(instrument, channel);
MidiOut.NoteOn(noteIndex, 100, channel.ToInt());
if (lastRotation != null) transform.rotation = lastRotation;
Quaternion rotation;
if (Mathf.Abs(transform.position.x - arm.transform.position.x) > Mathf.Abs(transform.position.z - arm.transform.position.z)) {
if (transform.position.x < arm.transform.position.x) {
rotation = Quaternion.Euler(0, 0, 90);
}
else {
rotation = Quaternion.Euler(0, 0, -90);
}
}
else {
if (transform.position.z < arm.transform.position.z) {
rotation = Quaternion.Euler(-90, 0, 0);
}
else {
rotation = Quaternion.Euler(90, 0, 0);
}
}
rotation *= transform.rotation;
lastRotation = rotation;
transform.DORotateQuaternion(rotation, 0.4f).SetEase(Ease.OutBack);
```

I've added a few extra things, but the core is essentially the same. What changed was: instead of initially setting rotation to be transform.rotation, and then multiplying it by the rotation I actually wanted, I did it in reverse. I set the rotation variable to the desired rotation, and then at the very end I applied it to transform.rotation.

Doing the multiplication in the reverse order like this has the effect of making the rotation in WORLD space, not LOCAL space, so the object's previous rotation doesn't have an effect on its future rotations.

Thanks to @kryzodoze for sending me in the right direction.

**Answer** by kryzodoze
·
Sep 17, 2017 at 04:08 AM

But it seems like no matter what methods I try, the best case scenario is that it works, but it gets really strange when you rotate one axis and then the other, or rotate the x axis beyond 360 degrees.

If you rotate one axis, rotating another axis will always be relative to the new rotation. Try rotating the x-axis 90 degrees in the editor, and then applying your y or z rotation. Is it what you want? If yes, then you should be fine in the editor.

Rotating beyond 360 degrees should be fine, I believe Unity just mods it with 360 and uses the new value.

Can you explain a little more what exactly you are doing and seeing?

Here is the code I am using for this (forget the stuff about Midi)

```
public void OnHit(ArmBase arm) {
//text.enabled = true;
MidiOut.SetInstrument(instrument, channel);
MidiOut.NoteOn(noteIndex, 100, channel.ToInt());
var rotation = transform.rotation;
//if we're more aligned on the x axis, rotate along that axis
if (Mathf.Abs(transform.position.x - arm.transform.position.x) < Mathf.Abs(transform.position.z - arm.transform.position.z)) {
print("Aligned along x");
if (transform.position.x < arm.transform.position.x) {
rotation *= Quaternion.Euler(90, 0, 0);
}
else {
rotation *= Quaternion.Euler(-90, 0, 0);
}
}
//if we're more aligned on z axis, rotate along that axis.
else {
print("Aligned along z");
if (transform.position.z < arm.transform.position.z) {
rotation *= Quaternion.Euler(0, 0, -90);
}
else {
rotation *= Quaternion.Euler(0, 0, 90);
}
}
StartCoroutine(TweenRotate(rotation));
}
private IEnumerator TweenRotate(Quaternion rotation) {
for (float t = 0; t <= 1; t+=0.05f) {
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, t);
yield return new WaitForFixedUpdate();
}
}
```

I think you're definitely right, the problem is related to the fact that rotating along one axis and then along another doesnt quite work as I expected-- but I'm not sure how to adjust my rotation to account for the object's current rotation.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.