Search Unity

Question waiting for an animation

Discussion in 'Animation' started by The-Jackelope, Apr 11, 2022.

  1. The-Jackelope

    The-Jackelope

    Joined:
    Mar 23, 2022
    Posts:
    15
    surely I'm going about this all wrong and was wondering what better ways there are to wait for whole anims or parts of animations before calling functions

    so i'm trying to get actions happening throughout and after animations. However i find that i cannot get a very accurate and stable animation time to wait for ;

    i am using time.deltatime to affect my float within :
    waitforseconds(timeforanim*timedeltatime) i expected when 1 is multiplied by time.deltatime this would give the lenght of a frame

    because the deltatime changes depending on performance i cannot get a collider offset to line up with the animation properly.
    here is my code
    Code (CSharp):
    1.  Animator myAnimator;
    2. [SerializeField] float FramesforAnim=5f;
    3. CapsuleCollider2D mycCapsuleCollider;
    4. bool done=true;
    5. [SerializeField] float colliderYOffsetMax=0.76f;
    6. [SerializeField] float colliderYOffsetMin=-0.2f;
    7. [SerializeField] float ColliderScalingSpeed= 0.1f;
    8. [SerializeField] float Delta;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.    
    19.     }
    20.     private void Awake() {
    21.         myAnimator=GetComponent<Animator>();
    22.         mycCapsuleCollider=GetComponent<CapsuleCollider2D>();
    23.     }
    24.     private void OnCollisionEnter2D(Collision2D other) {
    25.         done=false;
    26.        
    27.         myAnimator.ResetTrigger("ON");
    28.         myAnimator.SetTrigger("ON");
    29.         StartCoroutine(Zap());
    30.     }
    31.  
    32.     IEnumerator Zap(){
    33.         yield return new WaitForSeconds(FramesforAnim*Time.deltaTime);
    34.         while(!done&& mycCapsuleCollider.offset.y<colliderYOffsetMax){
    35.             mycCapsuleCollider.offset= new Vector2(mycCapsuleCollider.offset.x,mycCapsuleCollider.offset.y+ColliderScalingSpeed);
    36.             yield return new WaitForEndOfFrame();
    37.         }
    38.         done=true;
    39.          while(done&& mycCapsuleCollider.offset.y>colliderYOffsetMin){
    40.             mycCapsuleCollider.offset= new Vector2(mycCapsuleCollider.offset.x,mycCapsuleCollider.offset.y-ColliderScalingSpeed);
    41.             yield return new WaitForEndOfFrame();
    42.         }
    43.     }
     
  2. SethMeshko

    SethMeshko

    Joined:
    Sep 20, 2014
    Posts:
    109
    So deltatime sucks for this because of its variability, use normalized:
    animNormalizedTime = animator.GetCurrentAnimatorStateInfo(0).normalizedTime
    I don't know if you know this, but delta and normalized return a whole number for each loop of the animation so you will want:
    normalizedTimeDecimal = animNormalizedTime % 1;

    Depending on how you are using this, you may want to consider scrapping this and just simply using events in the clip itself.