Reputation: 325
So I have got a drawer that is animated with a script that shows a panel to prompt the player to open/close the drawer using a trigger. I have also got a key with a similar script on it, but instead, it prompts the player to pick up the key also using a panel and triggers. I would like to place the key in the drawer, so when the player presses 'E' to open the drawer, they can also press 'E' to pick up the key. I have tried making the drawer's collider smaller and also made the key collider bigger but that just means the player can pick up the key without opening the drawer. What is a more effective way of achieving this? I will also attach my code if needed.
DrawerScript:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class DrawerScript : MonoBehaviour
{
UnityEvent Event = new UnityEvent();
public GameObject OpenPanel = null;
private bool _isInsideTrigger = false;
public Animator _animator;
public string OpenText = "Press 'E' to open drawer";
public string CloseText = "Press 'E' to close drawer";
private bool _isOpen = false;
private void Start()
{
OpenPanel.SetActive(false);
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = true;
OpenPanel.SetActive(true);
UpdatePanelText();
Event.Invoke();
}
}
private void UpdatePanelText()
{
Text panelText = OpenPanel.transform.Find("Text").GetComponent<Text>();
if (panelText != null)
{
panelText.text = _isOpen ? CloseText: OpenText;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = false;
OpenPanel.SetActive(false);
}
}
private bool IsOpenPanelActive
{
get
{
return OpenPanel.activeInHierarchy;
}
}
// Update is called once per frame
void Update()
{
if (IsOpenPanelActive && _isInsideTrigger)
{
if (Input.GetKeyDown(KeyCode.E))
{
_isOpen = !_isOpen;
Invoke ("UpdatePanelText", 1.0f);
_animator.SetBool("open", _isOpen);
}
}
}
}
Key Pickup Script:
using UnityEngine.UI;
using UnityEngine;
public class PickUp : MonoBehaviour
{
public Collider Collider;
public GameObject OpenPanel = null;
private bool _isInsideTrigger = false;
public string PickUpText = "Press 'E' to pick up";
private void Start()
{
if (OpenPanel != null)
{
OpenPanel.SetActive(false);
}
Collider = GetComponent<Collider>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = true;
OpenPanel.SetActive(true);
UpdatePanelText();
}
}
private void UpdatePanelText()
{
Text panelText = OpenPanel.transform.Find("Text").GetComponent<Text>();
if (panelText != null)
{
panelText.text = PickUpText;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = false;
OpenPanel.SetActive(false);
}
}
private bool IsOpenPanelActive
{
get
{
return OpenPanel.activeInHierarchy;
}
}
Upvotes: 1
Views: 78
Reputation: 629
You could use UnityEvent API.
It's not the best solution and there are several ways to do that but if you are trying to solve the problem quickly you could, for example, create a UnityEvent inside your drawer class.
You need to add the namespace:
using UnityEngine.Events;
and, as a variable:
UnityEvent event = new UnityEvent();
This will probably create a new event (very similar to a button) on your drawer class. Next step is creating a method that will enable or disable the collider, inside your key class. The method must me public.
After that, simply go to your drawer GameObject click on the "+" sign, drag your Key GameObject to it and select the class and the public method you just created. This will subscribe that method to the event.
Lastly, on your "open drawer" method, you simply call:
event.Invoke();
And everyone "listening" to it will be executed, including your class.
Upvotes: 1