0
\$\begingroup\$

I'm making a 2D platformer with Unity and I'm trying to create a pressure pad to open a door. I want the door to open when the player is on the pressure pad and close a few seconds after the player leaves it. I've tried different ways, but none of them have worked. In this case, the door should open upwards and close downwards. Also, I've already created some animations (idle, open, and close) for the door, but I don't know how to connect these two objects and then play the animations with proper timing. Thank you in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PressurePad: MonoBehaviour
{
    public UnityEvent OnActivate;
    public UnityEvent OnDeactivate;

    int objectsInContact;

    IEnumerator Timer()
    {  
        yield return new WaitForSeconds(6);
        OnDeactivate.Invoke();
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        objectsInContact++;
        if (objectsInContact == 1 && OnActivate != null) OnActivate.Invoke();
    }

    void OnTriggerExit2D(Collider2D other)
    {
        objectsInContact--;
        if (objectsInContact == 0 && OnDeactivate != null) OnDeactivate.Invoke();
        StartCoroutine(Timer());
      
    }

}

Note: This script was added shortly after the question was posted. Thanks to the first comment under the post, it is mostly made of the code from the answer of this question.

enter image description here

\$\endgroup\$
2

1 Answer 1

1
\$\begingroup\$

You're very close, but you're firing your deactivate event twice: once the instant the pad is empty, and then again after a delay.

Delete the first one, and also make sure you stop the timer if the player returns to the pad before the time is up:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.Events; 

public class PressurePad: MonoBehaviour {
 
    public UnityEvent OnActivate; 
    public UnityEvent OnDeactivate; 

    public float deactivationDelay = 6f;

    int objectsInContact;
    Coroutine waitingToDeactivate;


    IEnumerator Timer() { 
        yield return new WaitForSeconds(deactivationDelay);

        // Tell our listeners we're switched OFF.
        if (OnDeactivate != null) 
            OnDeactivate.Invoke(); 

        // Return to ready-to-activate state:
        waitingToDeactivate = null;
    } 

    void OnTriggerEnter2D(Collider2D other) { 
        objectsInContact++; 
        
        // Pressure pad pressed.
        if (objectsInContact == 1) {

            if (waitingToDeactivate != null) {
                // Cancel deactivation timer. 
                StopCoroutine(waitingToDeactivate);
                waitingToDeactivate = null;

            } else if (OnActivate != null) {
                // Tell our listeners we're switched ON.
                OnActivate.Invoke(); 
            } 
        }
    }

    void OnTriggerExit2D(Collider2D other) { 
        objectsInContact--; 

        // Pressure pad released.
        if (objectsInContact == 0) {
            // Start a delay before telling anyone.
            waitingToDeactivate = StartCoroutine(Timer()); 
        } 
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Thank you. This works better than the previous one. The only thing I need to fix is playing the animation (named OpenWhenOn) as long as the player is on the pressure pad. Should I use something like anim.Play ("OpenWhenOn"); in objectsInContact if statement? \$\endgroup\$
    – Ali
    Commented Dec 22, 2020 at 16:39
  • \$\begingroup\$ This sounds like a problem in your door state machine, not a problem that the pressure switch should know anything about. Want to post a new question, showing the animator control graph state machine you've set up for your door? \$\endgroup\$
    – DMGregory
    Commented Dec 22, 2020 at 16:40
  • \$\begingroup\$ Sure, I'll ask a new question. Thanks for everything. \$\endgroup\$
    – Ali
    Commented Dec 22, 2020 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .