Search Unity

Question Can't get Animation Trigger to Activate

Discussion in 'Animation' started by dvfaulkner56, Oct 11, 2022.

  1. dvfaulkner56

    dvfaulkner56

    Joined:
    Jan 14, 2022
    Posts:
    15
    My animation will run if I link the animation to Entry in the Animator. With the Default State link however, it never triggers. Here are my animator settings:
    upload_2022-10-11_0-2-18.png

    My trigger state condition is "StartPinWiper" and i have Has Exit Time turned off. Here is my Animation script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PinWiperAnimation : MonoBehaviour
    6. {
    7.     Animator myAnimator;
    8.     public BallReturn ballReturn;
    9.  
    10.     void Awake()
    11.     {
    12.         ballReturn = GetComponent<BallReturn>();
    13.     }
    14.  
    15.     const string SwipePins = "StartPinWiper";
    16.  
    17.     // Start is called before the first frame up
    18.     void Start()
    19.     {
    20.         myAnimator = GetComponent<Animator>();
    21.     }
    22.  
    23.     public void AnimationStatus()
    24.     {
    25.             if (ballReturn._ball == 0)
    26.             {
    27.                 myAnimator.ResetTrigger(SwipePins);
    28.             }
    29.  
    30.             else
    31.             {
    32.                 myAnimator.SetTrigger(SwipePins);
    33.             }
    34.     }
    35. }
    The trigger condition is based on if the ball (_ball) was thrown and collides with the floor and increments by one. Here is what should be calling the AnimationStatus() method and is responsible for the ball increment and status:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BallReturn : MonoBehaviour
    5. {
    6.     public int _ball = 0;
    7.     bool doUpdate = false;
    8.     public PinWiperAnimation pinWiperAnimation;
    9.  
    10.  
    11.     void Awake()
    12.     {
    13.         pinWiperAnimation = GetComponent<PinWiperAnimation>();
    14.     }
    15.  
    16.     void LateUpdate()
    17.     {
    18.         if (doUpdate)
    19.         {
    20.             _ball += 1;
    21.             _ball = _ball % 3;
    22.             StartCoroutine(DelayUpdate());
    23.         }
    24.  
    25.         doUpdate = false;
    26.     }
    27.  
    28.     public IEnumerator DelayUpdate()
    29.     {
    30.         yield return new WaitForSeconds(1.5f);
    31.         gameObject.SendMessage("UpdateScore", _ball, SendMessageOptions.RequireReceiver);
    32.         yield return 0;
    33.     }
    34.  
    35.     void OnTriggerEnter(Collider other)
    36.     {
    37.         if (other.gameObject.GetComponent<FixBallSpeed>() != null)
    38.         {
    39.             doUpdate = true;
    40.             other.gameObject.SendMessage("Reset", _ball, SendMessageOptions.DontRequireReceiver);
    41.             pinWiperAnimation.AnimationStatus();
    42.         }
    43.     }
    44.  
    45.     void ResetFrame()
    46.     {
    47.         foreach (var v in GameObject.FindGameObjectsWithTag("pin"))
    48.         {
    49.             v.SendMessage("ResetPin", (_ball), SendMessageOptions.DontRequireReceiver);
    50.         }
    51.         _ball = 0;
    52.     }
    53. }

    It does hit pinWiperAnimation.AnimationStatus(); but the animation does not start. I tried taking out the conditional logic and just had "myAnimator.SetTrigger(SwipePins);" but that did not work either. I made sure the script is attached and the _ball definitely increments and resets properly so i don't know what I am missing.



    Edit:

    So in the Animation script made a change, I tried doing the following:

    Code (CSharp):
    1.   void FixedUpdate()
    2.     {
    3.         if (ballReturn._ball != 0)
    4.         {
    5.             myAnimator.SetTrigger(PinWiper);
    6.         }
    7.  
    8.     }
    Now this still doesn't work correctly, but it helped me understand where the problem is. The variable _ball is not being updated in this script. If I change the bool to _ball != 1 the animation starts. So the question why doesn't the update method grab the _ball variables value as it changes? im 100% sure _ball increments...
     
    Last edited: Oct 12, 2022
  2. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    203
  3. dvfaulkner56

    dvfaulkner56

    Joined:
    Jan 14, 2022
    Posts:
    15
    Hi Marek,

    Could you look at my edit? I think i have identified the issue but am unsure how to fix it.