I am developing a simple 2D platform game with a minigame between levels (cause I started to learn Unity and C# just 4 months ago). The idea is that when the player approach certain object, a text appears telling that he has to press E to do the mini game. I am using OnTriggerStay2D, but it not always works, That is to say, the player enter de collider of the object, the text appear, but I press E and nothing happens, but if I try many times, at the end sometimes works and the mini game scene is loaded.
This is the code I am using:
public class Door1 : MonoBehaviour
{
public GameObject switchButton;
public GameObject door1Text;
public AudioSource test1Voice;
void Start()
{
door1Text.SetActive(false);
switchButton.SetActive(false);
}
private void OnTriggerStay2D(Collider2D player)
{
if (player.gameObject.CompareTag("Player"))
{
door1Text.SetActive(true);
switchButton.SetActive(true);
test1Voice.Play();
}
if (Input.GetKeyDown(KeyCode.E))
{
SceneManager.LoadScene("Neologism1");
}
}
private void OnTriggerExit2D(Collider2D player)
{
if (player.gameObject.CompareTag("Player"))
{
door1Text.SetActive(false);
switchButton.SetActive(false);
test1Voice.Stop();
}
}
This is the exact moment:
The player has a circle collider 2d, cuz de box colider cause me some problems with the movement (the player use to get stopped frequently).
If someone could give me a tip of what is happening I would be very grateful.
Anyway, thanks in adcanced to everyone.