Search Unity

How to set crossfade to not go past the end of the animation.

Discussion in 'Animation' started by Reedex, Jul 28, 2019.

  1. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    I have this code
    Code (CSharp):
    1. void Update () {
    2.         if (Input.GetKeyDown (KeyCode.Space)) {
    3.             Animator anim = gameObject.GetComponent<Animator> ();
    4.             anim.SetFloat ("Direction", 1);
    5.             //anim.Play ("coil", -1,0);
    6.             anim.CrossFade("coil",0,-1);
    7.         }
    8.         if (Input.GetKeyDown (KeyCode.Backspace))
    9.         {
    10.             Animator anim = gameObject.GetComponent<Animator> ();
    11.             anim.SetFloat ("Direction", -1);
    12.             //anim.Play ("coil", -1,1);
    13.             anim.CrossFade("coil",0,-1);
    14.  
    15.         }
    16.     }
    which reverses animation so i don't have to have 2. the problem with Play is , if i set normalized time to 0 for the normal speed its correct, same for the reversed 1(end of the animation) time. but if i switch fast it switches between the end and beggining of the animation immediately.so i thought of crossfade but that on the other hand "goes past the animation time"... and if i press space, let it sit for twenty seconds,then press backspace it takes those twenty seconds to reversely go back.(20s delay).. :- ) what am i doing wrong?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    I don't know of any convenient way to do this sort of thing properly with an Animator Controller, but this example explains how it can be done with my Animancer plugin (link in my signature).
     
  3. mountblanc

    mountblanc

    Joined:
    Sep 24, 2015
    Posts:
    93
    I had a same sort of problem with a 2d door system. Where also the doors would just knock everything aside when the animation was running.
    I replaced the animations by adding a Hinge Joined to the doors , set the angle limits and used motor speed to move the doors in the desired direction and turned the motors off when i wanted them to fall open again.
    And then Setting the Motor ->Maximum Motor force allowed me to fine tune the strengh of the doors so if their is an to heavy object the doors will stop moving until the blockage is removed.
    Now i got much better realistic door action then by using an animator.
    Not sure if this is any use in your case (as not sure how this all goes in 3d) and perhaps their are better alternatives but i myself am very happy with the results i got.
    upload_2019-7-28_11-29-55.png
    An other nice side effect is that if the cargo bin is filled with heavy objects the door starts to be forced open.
    Setting the motor force correctly makes the doors open a crack but still maintain closure.
     
  4. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    you are right that i dont have to do it through animation duh, i could just hard code it.