Search Unity

Question How to deactivate trigger for "X" amount of seconds

Discussion in 'Scripting' started by elcuci, Jun 19, 2021.

  1. elcuci

    elcuci

    Joined:
    Jun 13, 2021
    Posts:
    17
    I have this gate and I want it to open when you get close to it. So I have a script on a cube that acts as a trigger that plays the open and close animation when you touch it.
    The problem is every time you touch it it has to restart the animation.

    I had come up with a solution THAT WORKED!!! But I tried to add something and broke it and can't figure out how I did it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [code=CSharp]using System.Collections;
    6. using System.Collections.Generic;
    7. using UnityEngine;
    8.  
    9. public class TriggerOpen : MonoBehaviour
    10. {  
    11.     [SerializeField] private Animator myDoor = null;
    12.  
    13.     [SerializeField] private bool openTrigger = true;
    14.  
    15.     [SerializeField] private bool CoolBool = false;
    16.  
    17.  
    18.     private void OnTriggerEnter(Collider other)
    19.     {
    20.         if (other.CompareTag("Player"))
    21.         {
    22.             openTrigger = true;
    23.  
    24.             if (openTrigger)
    25.             {
    26.                 CoolBool = true;
    27.                 Invoke("SetBoolBack", 15.0f);
    28.             }
    29.                 if (CoolBool)
    30.             {
    31.                 openTrigger = false;
    32.                 myDoor.Play("Gate Open", 0, 0.0f);
    33.                 Invoke("SetBoolBackForTrigger", 15.0f);
    34.             }
    35.         }
    36.     }
    37.  
    38.     void SetBoolBack()
    39.     {
    40.         CoolBool = false;
    41.     }
    42.  
    43.     void SetBoolBackForTrigger()
    44.     {
    45.         openTrigger = true;
    46.     }
    47. }
    The idea is that when you enter the trigger it compares the player tag, if it is the player the bool "openTrigger" becomes true, which makes the bool "CoolBool" true as well (for 15 seconds according to the invoke function thing, that part works). "CoolBool" plays the animation and makes "openTrigger" false (which should make it so you can't trigger anything anymore and the animation doesn't keep restarting a bunch of time), then the invoke thing sets "openTrigger" back to true after 15 seconds, when the animation ends.

    I hope it was clear. If you have any doubts feel free to ask.
    Help pls I'm so sad cos I had made it work and broke it minutes later.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    I'm struggling to follow your logic there too unfortunately.

    You could use an Animation Event on the door open (right at the end) that re-enables clicking on the door.

    So the sequence would become:

    - door clickable
    - click it, mark it unclickable
    - animation plays
    - animation event fires at ends, makes door clickable again

    Google around for animation events. That way if you speed up or slow down the anim, it still will match.
     
    elcuci likes this.
  3. elcuci

    elcuci

    Joined:
    Jun 13, 2021
    Posts:
    17
    Thanks! Tutorials are terrible these days, animation events are clearly the way to go for this, I don't get why all the tutorials I watched never even mentioned them. And then I had to push my dumb brain to it's limit to come up with a dumb solution bc I didn't know all the possibilities.

    Anyways, one more step in the right direction. Thank you!