Comments and answers for "Trying to tween the rotation of a cube in x and z and having trouble"
http://answers.unity.com/questions/1408138/trying-to-tween-the-rotation-of-a-cube-in-x-and-z.html
The latest comments and answers for the question "Trying to tween the rotation of a cube in x and z and having trouble"Answer by TaylorAnderson
http://answers.unity.com/answers/1408497/view.html
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.Sun, 17 Sep 2017 23:50:34 GMTTaylorAndersonComment by TaylorAnderson on TaylorAnderson's answer
http://answers.unity.com/comments/1408320/view.html
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.Sun, 17 Sep 2017 14:41:44 GMTTaylorAndersonAnswer by kryzodoze
http://answers.unity.com/answers/1408153/view.html
> 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?Sun, 17 Sep 2017 04:08:54 GMTkryzodoze