Search Unity

Question Animation will only play 1 time.

Discussion in 'Animation' started by Huwey, Apr 14, 2023.

  1. Huwey

    Huwey

    Joined:
    Mar 17, 2019
    Posts:
    29
    So I have an animation that is supposed to play when you make a correct guess. It plays the first time but then doesn't play as it should after that.

    Here is a video of the animations running (2 separate animations that coincide with each other) and a video of the gameplay showing that it doesn't run the second time as it should. I have also noticed that the card on the right doesn't position itself correctly but watching the animation run it is placed back where it should be at.

    Game Play:


    As you can see the card isn't reset properly when it's moved back to the right side having to slide over to the right a little bit when the animation for a correct guess is ran the second time and the animation for the card moving doesn't happen at all as it being active is changed to false and the and is set to the left card and active.

    Animations:


    Here are the 2 scripts that I feel like would be the contributing factors but unsure where the issue is.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CardControl : MonoBehaviour
    6. {
    7.     public static int dealtCardNumber;
    8.     public static int newCardNumber;
    9.     public static bool guessLo = false;
    10.     public static bool guessHi = false;
    11.     public GameObject spaceShip;
    12.     public GameObject correctText;
    13.     public GameObject incorrectText;
    14.     public GameObject hiButton;
    15.     public GameObject loButton;
    16.     public GameObject dealButton;
    17.     public GameObject[] dealingLeftCards;
    18.     public GameObject[] turningRightCards;
    19.  
    20.     // Equal number equals a win
    21.     void Update()
    22.     {
    23.         if (guessHi == true)
    24.         {
    25.             guessHi = false;
    26.             hiButton.SetActive(false);
    27.             loButton.SetActive(false);
    28.             StartCoroutine(GuessingHi());
    29.         }
    30.         if (guessLo == true)
    31.         {
    32.             guessLo = false;
    33.             hiButton.SetActive(false);
    34.             loButton.SetActive(false);
    35.             StartCoroutine(GuessingLo());
    36.         }
    37.     }
    38.     IEnumerator GuessingHi()
    39.     {
    40.         yield return new WaitForSeconds(1);
    41.         if (newCardNumber >= dealtCardNumber)
    42.         {
    43.             correctText.SetActive(true);
    44.             spaceShip.SetActive(true);
    45.             Scoring.currentScore += 1;
    46.             yield return new WaitForSeconds(3);
    47.             for (int i = 2; i <= 14; i++)
    48.             {
    49.                 dealingLeftCards[i].SetActive(false);
    50.             }
    51.             dealingLeftCards[newCardNumber].SetActive(true);
    52.             for (int i = 2; i <= 14; i++)
    53.             {
    54.                 turningRightCards[i].SetActive(false);
    55.             }
    56.             yield return new WaitForSeconds(1);
    57.             hiButton.SetActive(true);
    58.             loButton.SetActive(true);
    59.             dealButton.SetActive(false);
    60.             correctText.SetActive(false);
    61.             spaceShip.SetActive(false);
    62.             dealtCardNumber = newCardNumber;
    63.         }
    64.         else if (newCardNumber <= dealtCardNumber)
    65.         {
    66.             incorrectText.SetActive(true);
    67.             Scoring.currentScore = 0;
    68.             yield return new WaitForSeconds(3);
    69.             for (int i = 2; i <= 14; i++)
    70.             {
    71.                 dealingLeftCards[i].SetActive(false);
    72.             }
    73.             for (int i = 2; i <= 14; i++)
    74.             {
    75.                 turningRightCards[i].SetActive(false);
    76.             }
    77.             yield return new WaitForSeconds(1);
    78.             dealButton.SetActive(true);
    79.             incorrectText.SetActive(false);
    80.         }
    81.     }
    82.  
    83.     IEnumerator GuessingLo()
    84.     {
    85.         yield return new WaitForSeconds(1);
    86.         if (newCardNumber <= dealtCardNumber)
    87.         {
    88.             correctText.SetActive(true);
    89.             spaceShip.SetActive(true);
    90.             Scoring.currentScore += 1;
    91.             yield return new WaitForSeconds(3);
    92.             for (int i = 2; i <= 14; i++)
    93.             {
    94.                 dealingLeftCards[i].SetActive(false);
    95.             }
    96.             dealingLeftCards[newCardNumber].SetActive(true);
    97.             for (int i = 2; i <= 14; i++)
    98.             {
    99.                 turningRightCards[i].SetActive(false);
    100.             }
    101.             yield return new WaitForSeconds(1);
    102.             hiButton.SetActive(true);
    103.             loButton.SetActive(true);
    104.             dealButton.SetActive(false);
    105.             correctText.SetActive(false);
    106.             spaceShip.SetActive(false);
    107.             dealtCardNumber = newCardNumber;
    108.         }
    109.         else if (newCardNumber >= dealtCardNumber)
    110.         {
    111.             incorrectText.SetActive(true);
    112.             Scoring.currentScore = 0;
    113.             yield return new WaitForSeconds(3);
    114.             for (int i = 2; i <= 14; i++)
    115.             {
    116.                 dealingLeftCards[i].SetActive(false);
    117.             }
    118.             for (int i = 2; i <= 14; i++)
    119.             {
    120.                 turningRightCards[i].SetActive(false);
    121.             }
    122.             yield return new WaitForSeconds(1);
    123.             dealButton.SetActive(true);
    124.             incorrectText.SetActive(false);
    125.         }
    126.     }
    127.  
    128. }\

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveRightCard : MonoBehaviour
    6. {
    7.     private Animator anim;
    8.     public GameObject correctText;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         anim = gameObject.GetComponent<Animator>();
    14.     }
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         anim.speed = 0f;
    19.         StartCoroutine(CorrectGuess());
    20.     }
    21.     IEnumerator CorrectGuess()
    22.     {
    23.         if (correctText.activeInHierarchy == true)
    24.         {
    25.             anim.Play("MoveRightCard");
    26.             anim.speed = 1f;
    27.             yield return new WaitForSeconds(3);
    28.         }
    29.     }
    30. }
    Any help on what could be the issue would be greatly appreciated. Also if there is a better way to have these 2 scripts so that they work better that would also be appreciated.
     
  2. ElXill

    ElXill

    Joined:
    Jun 2, 2017
    Posts:
    43
    This is kinda a hunch but it feels like when your first animation ends it plays 3 seconds of your animation in coroutine and when second time you play same animation, it continues from the 4th second, where it left off (3th second) plus 1 second offset.
    Tbh, I didn't examine your code very well but I am saying these mosty by looking at your videos.
     
  3. olivia3212

    olivia3212

    Joined:
    Apr 15, 2023
    Posts:
    3
    Hello Huway,
    Based on the information you've provided, it's difficult to pinpoint the exact issue without more details about the behavior you're experiencing. However, there are a few things you could try to help troubleshoot the problem:

    1. Make sure that all of your variables and states are being reset properly. It's possible that something is carrying over from one animation to the next, causing unexpected behavior.

    2. Check the order of your animations and make sure they are playing in the correct sequence. It's possible that one animation is blocking the other from playing correctly.

    3. Try simplifying your code and breaking it down into smaller functions that you can test individually. This can help you isolate the problem and determine where the issue is coming from.

    4. Consider using an animation controller or state machine to manage your animations. This can help you better control the sequence of your animations and ensure that they are playing correctly.

    5. Test your code in a clean environment, such as a new project or scene, to rule out any external factors that may be affecting your animations.
    I hope these tips help you troubleshoot the issue you're experiencing. Good luck!

    [Best Regards]
    Olivia Devid