Search Unity

prevent player input before animation finish?

Discussion in 'Animation' started by Deleted User, Jul 29, 2021.

  1. Deleted User

    Deleted User

    Guest

    I did a door opening and closing animation. But when the door opens, if the player enters a input, the closing animation comes into play and continues by repeating itself. What I want to do is block the input from the user until the animation is finished.
    Code (CSharp):
    1. public class DoorControl : MonoBehaviour
    2. {
    3.     public Animator ThisAnimator;
    4.     private bool correction = false;
    5.     public AudioSource ThisAudioClose;
    6.     public AudioSource ThisAudioOpen;
    7.     private bool isDoorOpen;
    8.  
    9.     private void Update() {
    10.         if(correction)
    11.         {
    12.             if(Input.GetKeyDown(KeyCode.E))
    13.             {
    14.                 if(isDoorOpen == false)
    15.                 {
    16.                     ThisAnimator.SetTrigger("TriggerDoor");
    17.                     ThisAudioOpen.Play();
    18.                     isDoorOpen = true;
    19.                 }
    20.                 else
    21.                 {
    22.                     ThisAnimator.SetTrigger("TriggerDoor");
    23.                     ThisAudioClose.Play();
    24.                     isDoorOpen = false;
    25.                 }
    26.             }
    27.         }
    28.     }
    29.     private void OnTriggerEnter(Collider other) {
    30.         correction = other.CompareTag("Player");
    31.     }
    32.  
    33.     private void OnTriggerExit(Collider other) {
    34.         correction = false;
    35.     }
    36.  
    37.  
    38. }
    ss.png
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    My personal preference when doing stuff like this is to use animation events: https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html

    When you start the DoorOpen animation in code, disable the player's inputs however you like (disable the "PlayerController" script or something similar).

    Create a method in the DoorControl script that enables the player's inputs when called (EnablePlayerInput for example), and create an animation event at the end of the DoorOpen animation that calls EnablePlayerInputs.

    Just keep in mind that the script with the method you call from the animation event needs to be on the same object as the animator.
     
    Deleted User likes this.