Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using Coroutines to create a fnaf-esc monitor flip up animation

Discussion in 'Scripting' started by yosheDev, Dec 3, 2020.

  1. yosheDev

    yosheDev

    Joined:
    Mar 28, 2020
    Posts:
    5
    Or more specifically, to wait until the animation has finished before changing booleans.

    Hey all! I've been trying to create a fnaf-esc "monitor flip up" affect for my paranormal investigation game. I first tried to see if it was possible to detect whether or not a certain animation has finished, only to find forums that widely suggest that instead using a coroutine with a timer of the same length as the animation was a better solution.

    So here I find myself trying to learn how coroutines work. I am also new to the Animator system in unity, but all of my animation states are in there.

    I'm asking for advice on what approach would be the best to achieve this affect?

    My code is the following:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6.  
    7. public class openCams : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    8. {
    9.     public int gameState;
    10.  
    11.     private CanvasGroup mouseControls;
    12.     private CanvasGroup minimapGroup;
    13.  
    14.     public Animator monitorAnimator;
    15.  
    16.     [SerializeField]
    17.     private bool flipToggle = false;
    18.     [SerializeField]
    19.     private bool isCamsOpen = false;
    20.  
    21.     private float monitorAnimTime = 1f;
    22.  
    23.     void UpdateAnimator()
    24.     {
    25.         monitorAnimator.SetBool("flipToggle", flipToggle);
    26.         monitorAnimator.SetBool("isCamsOpen", isCamsOpen);
    27.     }
    28.  
    29.     // -=- Utility Methods -=-
    30.  
    31.     void CheckMonitorState()
    32.     {
    33.         if (isCamsOpen == false && IsAnimPlaying(monitorAnimator, "Up"))
    34.         {
    35.             isCamsOpen = true;
    36.         }
    37.         if (isCamsOpen = true && IsAnimPlaying(monitorAnimator, "Down"))
    38.         {
    39.             isCamsOpen = false;
    40.         }
    41.     }
    42.     private IEnumerator FlipMonitor()
    43.     {
    44.         //Wait animation time length
    45.         //is waiting whole time to even do the animation
    46.         UpdateAnimator();
    47.  
    48.         //Monitor should be opening BEFORE the WaitForSeconds but it isnt
    49.         yield return new WaitForSeconds(monitorAnimTime);
    50.  
    51.         //isCamsOpen = IsAnimPlaying(monitorAnimator, "Up");
    52.         UpdateAnimator();
    53.         yield return null;
    54.  
    55.         if (isCamsOpen == false)
    56.         {
    57.             minimapGroup.alpha = 0f;
    58.             if (IsAnimPlaying(monitorAnimator, "flipUp"))
    59.             {
    60.                 Debug.Log("Flipping Up");
    61.                 //isCamsOpen = IsAnimPlaying(monitorAnimator, "Up");
    62.                 yield return null;
    63.                 isCamsOpen = true;
    64.             }
    65.             else if (IsAnimPlaying(monitorAnimator, "Down"))
    66.             {
    67.                 Debug.Log("Down");
    68.                 isCamsOpen = false;
    69.                 yield return null;
    70.             }
    71.             else if (IsAnimPlaying(monitorAnimator, "flipDown"))
    72.             {
    73.                 Debug.Log("Flipping Down");
    74.                 //isCamsOpen = IsAnimPlaying(monitorAnimator, "Up");
    75.                 yield return null;
    76.                 isCamsOpen = false;
    77.             }
    78.             else
    79.             {
    80.                 Debug.Log("Big boy error");
    81.                 yield return null;
    82.             }
    83.         }
    84.         else if (isCamsOpen == true)
    85.         {
    86.             minimapGroup.alpha = 1f;
    87.             flipToggle = false;
    88.             Debug.Log("Cams open true");
    89.         }
    90.         UpdateAnimator();
    91.     }
    92.  
    93.     bool IsAnimPlaying(Animator givenAnimator, string givenState)
    94.     {
    95.         if (givenAnimator.GetCurrentAnimatorStateInfo(0).IsName(givenState))
    96.         {
    97.             return true;
    98.         }
    99.         else
    100.         {
    101.             return false;
    102.         }
    103.     }
    104.  
    105.     public void OnPointerEnter(PointerEventData eventData)
    106.     {
    107.         if (gameState == 1)
    108.         {
    109.             if (flipToggle == false)
    110.             {
    111.                 flipToggle = true;
    112.                 // At this point - flip toggle true, is opened false
    113.                 StartCoroutine("FlipMonitor");
    114.                 Debug.Log("Exit Coroutine");
    115.                 //UpdateAnimator();
    116.             }
    117.         }
    118.     }
    119.  
    120.     public void OnPointerExit(PointerEventData eventData)
    121.     {
    122.         if (gameState == 1)
    123.         {
    124.             Debug.Log("Mouse exit");
    125.         }
    126.     }
    127. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Havyx, yosheDev and Kurt-Dekker like this.
  3. yosheDev

    yosheDev

    Joined:
    Mar 28, 2020
    Posts:
    5
    YES!! State machines is exactly the kind of thing I was looking for. It's remarkable how none of the other forum posts I found had even the slightest of mention to this. I was able to look into them and resolve every issue I was facing. Thank you :)
     
    PraetorBlue likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Awesome! Glad you got it working.
     
    yosheDev likes this.