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

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.

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?