Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Time-Based Animation

Discussion in 'Scripting' started by marty, Jul 6, 2005.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Could someone please do me a huge favor and post a simple template of time-based animation in Unity? I'm assuming that since Unity is (by default) frame-based, we should be doing all of our animation with a time-factor.

    Nothing fancy - just a simple transform would be sufficient. I just need to see how you calculate and introduce the time-based factor.

    I'm not sure when I use deltaTime and fixedDelteTime and whetrher there are differences in the implementation depending on whether or not there is physics going on.

    Also, please be sure that the example both caps the speed of faste machines and adjusts the animation steps for slower ones.

    Thanks!
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    * When changing a transform you always want to multiply by Time.deltaTime.

    Code (csharp):
    1.  
    2. function Update () {
    3.   transform.position.x += 0.1 * Time.deltaTime;
    4. }
    5.  
    You should multiply by Time.deltaTime, no matter if you do it inside of a FixedUpdate function, Coroutine or Update function.

    Code (csharp):
    1.  
    2. function FixedUpdate () {
    3.   transform.position.x += Input.GetAxis ("Horizontal") * Time.deltaTime;
    4. }
    5.  
    The FixedUpdate function is called at fixed time intervals and Time.deltaTime will return the fixed time interval when you query it from inside FixedUpdate.
    It is not strictly necessary to multiply with Time.deltaTime when inside a FixedUpdate function but it is good practice since, it will allow you to adjust the fixed time step variable in the Time settings later on and your simulation will look the same.


    There is one exception when you don't want to multiply by delta time when changing a Transform.

    MouseDelta.
    The mouse delta is how much the mouse has moved during the last frame. So it is independent of time and should not be multiplied with Time.deltaTime
    Code (csharp):
    1.  
    2. function Update () {
    3.    transform.Rotate (0, Input.GetAxis ("Mouse X"), 0);
    4. }
    5.  
    * Some other variables which you animate should also be multiplied with delta time. For example if you want to change the range of a light, you should multiply by delta Time.

    Code (csharp):
    1.  
    2. function Update () {
    3.    light.range += 0.1 * Time.deltaTime;
    4. }
    5.  

    * When working with rigid bodies you should not multiply with Time.deltaTime.
    Code (csharp):
    1.  
    2. function FixedUpdate () {
    3.    rigidbody.AddForce (Vector3.up);
    4. }
    5.  
    Will always do the same no matter the speed of your computer.
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Perfect! Thanks, Joe.

    Seems like a great implementation too - no more calculating time-based animation factors on the fly - woohoo!

    You guys really thought this Unity thing out, didn't you!