Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotation Problem

Discussion in 'Scripting' started by AllanMSmith, Feb 12, 2015.

  1. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Hey everyone,

    Im running once again into a problem with rotating objects through code. Here is my case:

    I have a script that helps me make GFXs. Often times, I have an object, like a "disc", that I want to spin around itself. I know the starting euler angles (from inspector), I know the final euler angles (from inspector as well), and I know how long the interpolation should be. However, as you might guess, interpolating euler angles to rotate doesnt work as planned and the object will make strange angles along the way.

    Problem is, the same happens using an Animation Clip. And if I use quaternions, it does work without strange angles, but not as expected, because the Quaternion.Lerp will use the SHORTER way to rotate towards the target rotation... which is a big problem, for example, when making a "slash" that rotates 200 degrees, slashing in the front of the character, the object rotates 160 through the back...

    So... what is the best way to rotate something, either through code or Unity's animation system, in a way that I can rotate it as much as I want, in whatever angles I want, and making it possible to make it rotate 360 degrees, or who knows, even more?

    Thanks!
     
  2. Megolas

    Megolas

    Joined:
    Apr 6, 2013
    Posts:
    25
    Can't see why it wouldn't work with euler angles... Can you post the code you used?
     
  3. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Sure, its not a direct "lerping" but here it is:

    First, in editor time, I set the target euler angles (there is a baking process because this same script is responsible for a lot of stuff like lerping positions, material colors, etc etc). I triple checked and this part works correctly.

    Then, on runtime, I have the following:
    Code (CSharp):
    1.                         if (CurveList[i].X)
    2.                             _currentTargetEulerAngles.x = Mathf.Lerp(_originalLocalEulerAngles.x, _targetEulerAngles.x, curveValue);
    3.                         if (CurveList[i].Y)
    4.                             _currentTargetEulerAngles.y = Mathf.Lerp(_originalLocalEulerAngles.y, _targetEulerAngles.y, curveValue);
    5.                         if (CurveList[i].Z)
    6.                             _currentTargetEulerAngles.z = Mathf.Lerp(_originalLocalEulerAngles.z, _targetEulerAngles.z, curveValue);
    Initially I set "_currentTargetEulerAngles" to be = _targetEulerAngles, so by checking if the rotation curve should apply X, Y or Z, I only change that value, that is why Im lerping each axis in a different line. The reason for this mess is that I use animation curves so if I want to make something that rotates a lot on the beginning and slows down... or rotates all the way and then rotates back... I can control these kind of behaviors freely in the curves.

    Then, with the "_currentTargetEulerAngles" (which I did triple check as well), I do the following:
    Code (CSharp):
    1.                 _transform.localRotation = Quaternion.Euler(_currentTargetEulerAngles);
    which is just directly applying, as the actual "Lerping" happens above.

    The whole system works, and by DebugLogging the value of _currentTargetEulerAngles I know it is lerping correctly, however when this value is applyed to the rotation, the object rotates... strangely, much similar to the effect described in this image: