Search Unity

Animation only plays once

Discussion in 'Animation' started by blopin, Aug 17, 2021.

  1. blopin

    blopin

    Joined:
    Apr 23, 2021
    Posts:
    2
    I know this question has been answered many times before, but I still can't manage to get my script to work. I'm developing a CPR game and every time the space bar is pressed, it should trigger the hand compression animation. I set all my loops to off and I have an idle state. Sorry for the large block of code, line 13 is where it's triggered.

    Code (CSharp):
    1.  
    2. void Update()
    3.     {
    4.         if (Tempo.activeSelf)
    5.         {
    6.             if (Input.GetKeyDown(KeyCode.Space))
    7.             {
    8.                 if (check == false)
    9.                 {
    10.                     StartCoroutine(bpmTimer());
    11.                     check = true;
    12.                 }
    13.  
    14.                 countCompressions--;
    15.                 compressionAnim.Play("HandCompressionAnimation");
    16.  
    17.                 if (countCompressions > 0)
    18.                 {
    19.                     Debug.Log("Counting compressions");
    20.                 }
    21.  
    22.                 if (countCompressions <= 0)
    23.                 {
    24.                     StopAllCoroutines();
    25.  
    26.                     if (openWindow == true  && endTempoPanel == false)
    27.                     {
    28.                         Debug.Log("Correct bpm");
    29.                         endTempoPanel = true;
    30.                         Tempo.SetActive(false);
    31.                         HandAnimation.SetActive(false);
    32.                         RescueBreaths.SetActive(true);
    33.                     }
    34.                     else if (tooFast == true && endTempoPanel == false)
    35.                     {
    36.                         Debug.Log("Too fast");
    37.                         endTempoPanel = true;
    38.                         Tempo.SetActive(false);
    39.                         HandAnimation.SetActive(false);
    40.                         FailureToCompressFast.SetActive(true);
    41.                     }
    42.                     else if (tooSlow == true && endTempoPanel == false)
    43.                     {
    44.                         Debug.Log("Too slow");
    45.                         endTempoPanel = true;
    46.                         Tempo.SetActive(false);
    47.                         HandAnimation.SetActive(false);
    48.                         FailureToCompressSlow.SetActive(true);
    49.                     }
    50.                 }
    51.             }
    52.             else
    53.             {
    54.                 compressionAnim.Play("Idle");
    55.             }
    56.         }
    57.  
    58.         GameObject HeadAreaTrigger = GameObject.Find("HeadAreaTrigger");
    59.         TriggerAreaScript triggerAreaScript = HeadAreaTrigger.GetComponent<TriggerAreaScript>();
    60.         //triggerArea1 = triggerAreaScript.triggerArea;
    61.  
    62.         if (triggerAreaScript.triggerArea == true && checkedVitals == false)
    63.         {
    64.             Debug.Log("Calling checked vitals function");
    65.             onCheckedVitals();
    66.             checkedVitals = true;
    67.         }
    68.     }
     
  2. blopin

    blopin

    Joined:
    Apr 23, 2021
    Posts:
    2
    I'm an idiot, just needed to check exit time and make a transition from idle to animation and back. As you might be able to tell I'm new at this.
     
unityunity