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

How to animate a non-linear rotation from scripts?

Discussion in 'Animation' started by cvbattum, Apr 20, 2018.

  1. cvbattum

    cvbattum

    Joined:
    Apr 20, 2018
    Posts:
    12
    For a 2D game I'm making I have a circular selection wheel that you control with the mouse. There's a needle in the center that points to which object you've selected. It's essential to note that this selection wheel is used throughout multiple scenes where it can have a variable number of elements. The needle snaps to the selected element instead of always following the cursor.

    Right now I'm instantly setting the rotation of the needle by changing the eulerAngles variable in the Update() method. What I want instead is to animate the needle rotating. I know I can do this using a linear interpolation function, but a linear animation would not look well for what I'm making. Instead I want a strong 'ease-out' animation, very fast at the beginning and only going slower very close to the destination. I think if I'd spend enough time on it I could program it, but I was hoping it could be done with the animation functionality within Unity.

    The only problem is that I have essentially no experience with the animation system. I tried to animate a linear function earlier for the same problem but I ended up putting hours into something that in the end still wouldn't work. I know how to make an animation clip in the editor. My question is how to then run this clip from a script. Also, how would I make this clip fit every single rotation action that this needle will perform? If I have four elements, it needs to work for rotation from 0 to 90 degrees, but also 180 to 270. The clip also needs to work for a selection wheel that has for example five elements (where the angle between needle positions is 72 instead of 90). Is this possible to do? And if so, how? When I googled for this, I almost only found tutorials explaining how to do this with just the animation functionality and barely using scripts. If you know any good tutorials that clearly explain what I want, please post those too.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    You can use Apply Root Motion in your Animator, then the rotation from your Animation Clip will be relative to the current rotation. You need to do a Generate Root Motion Curve on your animation clip also.
     
    theANMATOR2b likes this.
  3. cvbattum

    cvbattum

    Joined:
    Apr 20, 2018
    Posts:
    12
    How do I specify how many degrees the needle needs to rotate? Currently I can only use absolute values but like I said, I need to be able to go from 10 to 20 (10 deg) or from 200 to 3 (163 deg) if I need to.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    I kind of believe that this would be easier to solve using a script, but it's an interesting challenge. So, just for fun, let's see what we can do :) (This might not be the easiest way to do this, but...)

    So, make two animations. One empty clip, with nothing at all in it. One clip with a rotation from 0 to 360 degs. Make the latter one second long.

    Uncheck Loop Time in both clips.

    On the rotation clip, check the Generate Root Motion Curves at Runtime.

    Check Apply Root Motion in the Animator.

    Set the empty clip as the default in your Animator.

    Make a Transition from the empty clip to the rotation. Turn off everything for this Transition, that is; Exit Time, Fixed Duration, and so on. Add a Trigger to the Conditions.

    Make a Transition from the rotation clip to the empty one. Check Has Exit Time. Uncheck Fixed Duration and set Transition stuff to 0.

    If we run this, then when the trigger is triggered the object with the Animator will spin a complete 360. If we want it to spin 180 deg, then we change the Exit Time to 0.5, the the clip will exit after half the time. This means that it only will have the time to spin 180 deg. So, setting the Exit Time to degreesToSpin/360 will make it spin a certain amount.

    This might not be a great solution, but it was fun to figure out :)