I'm trying to make a simple game with some cubes rotating 90º when the user clicks on them.

I've setup an empty game object, a simple cube nested, an animator, and two animations: RotateClockWise, RotateCounterClockwise.

The idea is: User clicks, the nested cube rotates 90 degrees, then comunicates the new translate component to the wrapper and the wrapper updates its transform (I'm doing this so I can have only two animations). So far, so good.

The thing is that I've noticed that, even though the animation is "90º", once I've clicked enough times, a small offset begins to accumulate and ends up, yeah, rotating 90ºm but starting on 20º, so I got a not aligned cube.

The "dirty" trick I've thought is doing a manual round to the closest quarter on each turn, but that seems something I shouldn't be doing, given how advanced is supossed to be this system.

Any idea of what's going on?

EDIT: This is the math on teh "endAnimation()" function that updates the parent object with the rotation of the child:

        float newX = _rootGameObject.transform.rotation.eulerAngles.x + gameObject.transform.localRotation.eulerAngles.x;
        float newY = _rootGameObject.transform.rotation.eulerAngles.y + gameObject.transform.localRotation.eulerAngles.y;
        float newZ = _rootGameObject.transform.rotation.eulerAngles.z + gameObject.transform.localRotation.eulerAngles.z;
        _rootGameObject.transform.rotation = Quaternion.Euler(new Vector3(newX, newY, newZ));
  • 2
    \$\begingroup\$ Sounds like accumulating rounding errors. Can you show us the code you're using? \$\endgroup\$ – DMGregory May 2 '18 at 0:22
  • \$\begingroup\$ There's no math involved on the code, only setters, getters and triggers for the animations (Whose transforms go from 0º to 90º). Someone on Twitter told me to check transitions and such, just in case one animation launches before the previous finishes, and it seems this has alleviate the problem, but it still happens. \$\endgroup\$ – Neuromante May 3 '18 at 9:03
  • \$\begingroup\$ You don't have to write the math yourself for math to happen. All animations and transformations in a game engine boil down to math. So the simple choice of setting a variable following an animation is also math, and the particular order or stacking of operations can impact the result. That's why I've asked to see a sample of your code — it makes a difference. \$\endgroup\$ – DMGregory May 3 '18 at 10:41
  • \$\begingroup\$ Ah, sorry for the late answer, but time hasn't been a commoditty I'd have these last days. I've updated the original post with the "endAnimation()" function I was using.Basically, I was adding eulerangles.component. \$\endgroup\$ – Neuromante May 8 '18 at 19:52
  • \$\begingroup\$ Ah. Calculating new rotations by doing math on Euler angles is almost never what you want. Do you get the same issue with the one-liner _rootGameObject.transform.rotation = gameObject.transform.rotation; ? \$\endgroup\$ – DMGregory May 8 '18 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.