Home > Back-end >  GetKeyDown not executing inside onTriggerStray2D, Unity C#
GetKeyDown not executing inside onTriggerStray2D, Unity C#

Time:07-17

I am trying to make an object that teleports the player to a different scene when clicked. I tried following this tutorial: https://youtu.be/PpLJq6AR2J0 This is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Table : MonoBehaviour
{
    [SerializeField] private GameObject UiElement;

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            UiElement.SetActive(true);
            if (Input.GetKeyDown(KeyCode.E))
            {
               SceneManager.LoadScene("Table1");
            }
        }
        
    }

    void Update()
    {
        
    }


    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            UiElement.SetActive(false);
        }
    }
}

But when I tried it executes everything except

if (Input.GetKeyDown(KeyCode.E))
            {
               SceneManager.LoadScene("Table1");
            }

The code is correct and it executes when it's inside the update function. Is there a fix to this?

CodePudding user response:

The physics framerate update doesn't have to match the game framerate. The Input.GetKeyDown will be true only for one frame so it won't be called. You should put input checks like Input.GetKeyDown inside the update function.

This is not the best way to do it but only to show a way to do it:

 private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            UiElement.SetActive(true);
            colliderHit = true;   

        }
        
    }

    void Update()
    {
          if (Input.GetKeyDown(KeyCode.E))
        {
          if(colliderHit)
            SceneManager.LoadScene("Table1");
        }
          
    } 

CodePudding user response:

Thanks, here is the solution:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Table : MonoBehaviour
{
    private bool collided = false;
    [SerializeField] private GameObject UiElement;

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            collided = true;
        }
        
    }

    void Update()
    {
        if (collided == true)
        {
            UiElement.SetActive(true);
            if (Input.GetKeyDown(KeyCode.E) && UiElement == true)
            {
                SceneManager.LoadScene("Table1");
            }
        }
    }


    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            collided = false;
            UiElement.SetActive(false);
        }
    }
    
}

I just used a boolean called collided. That way u can check inside the Update() function.

  • Related