Access Denied: Locked Door Interactions
Locked doors that give access to new areas are very commonplace in video games. This article continues on from my Door Interactions article, and dives deeper into locked and unlocked door mechanics.
Previous article on doors below.
The Interactable script on the door remains the same, but the Door script now has references to a Mesh Renderer and a Material that will be used to show the player visually when the door has been unlocked. The cube primitive is the object being used to change from red to green, when a door becomes unlocked.
Here is the red emissive cube being used as the visual communication with the player regarding the doors locked status.
The Console object is what can be interacted with to unlock the doors. It has the same Interactable script as the other interactable objects, but also has it’s own simple Terminal script attached.
When the player tries to interact with a locked door, this UI display appears letting them know to use the Console to unlock the doors. The Text will fade out after a few seconds.
Here is the Console object showing it’s interaction UI.
Scripting
While the interactable script handles the UI display and input interaction for the Console, the Terminal class has a single event that gets called when this object is interacted with.
using UnityEngine;
using System;
public class Terminal : MonoBehaviour, iInteractable
{
public static event Action onTerminalInteraction;
public void Interact()
{
//unlock doors
onTerminalInteraction?.Invoke();
}
}The Door class has some new variables to handle it’s locked behavior, as well as the red/ green visuals.
public class Door : MonoBehaviour, iInteractable
{
[SerializeField] private bool _isOpen = false;
private bool _isLocked = true;
private Animator _animatior;
[SerializeField] private GameObject _lockMessageUI;
private WaitForSeconds _waitForSeconds;
private bool _isAnimating = false;
[SerializeField] GameObject _accessLight;
[SerializeField] private MeshRenderer _mr;
[SerializeField] private Material _emissiveMat;
private const string _emissiveColor = "_EmissiveColor";
}Here in void Start, we get the material instance on the cube object (access light), and subscribe to the OnTerminalInteraction event coming from the Console.
private void Start()
{
_animatior = GetComponent<Animator>();
_waitForSeconds = new WaitForSeconds(5.1f);
if (_animatior == null)
Debug.LogWarning("The Animator on the Door " + gameObject.name + " is NULL");
Terminal.onTerminalInteraction += Terminal_onTerminalInteraction;
_mr = _accessLight.GetComponent<MeshRenderer>();
//get material
if (_mr != null)
{
_emissiveMat = _mr.material;
}
else
Debug.LogWarning("The Mesh Renderer on the Door Access object is NULL");
}The OnDisable method unsubscribes from the event, while the event method itself unlocks the door and sets the base color and emission HDR color to Green.
private void OnDisable()
{
Terminal.onTerminalInteraction -= Terminal_onTerminalInteraction;
}
private void Terminal_onTerminalInteraction()
{
_isLocked = false;
_emissiveMat.color = Color.green;
_emissiveMat.SetColor(_emissiveColor, Color.green);
}The Interact method now has a check for the isLocked bool before opening the door. If the door is locked, the UI message will display and an animation will fade the text out after a few seconds. The coroutine simply sets the object back to disabled after the elapsed time. The animation plays on awake, so the text is ready again should the player continue to try accessing a locked area.
public void Interact()
{
if (_isLocked)
{
//show lock message to disable after a time and fade out
if (_lockMessageUI != null)
{
_lockMessageUI.SetActive(true);
if (!_isAnimating)
{
_isAnimating = true;
StartCoroutine(DeactivateUIRoutine());
}
}
}
else
{
_isOpen = !_isOpen;
_animatior.SetBool("IsOpen", _isOpen);
}
}
private IEnumerator DeactivateUIRoutine()
{
yield return _waitForSeconds;
if (_lockMessageUI != null)
{
_lockMessageUI.SetActive(false);
}
_isAnimating = false;
}Not yet you don’t!
Now that the console has been interacted with, the doors have been unlocked as shown by the color change on the panel.
Access granted!
Please join me for my next article where I discuss physics based pressure plates. Thanks for reading!
