Home > OS >  Input not registering in OnCollisonEnter2D,OnCollisionExit2D and OnCollisionStay2D
Input not registering in OnCollisonEnter2D,OnCollisionExit2D and OnCollisionStay2D

Time:02-21

I am trying to get input when my player collides with another object. But the problem is i am not getting any input. I am getting inputs in update function but not in OnCollisonEnter2D,OnCollisionExit2D and OnCollisionStay2D. Here is my script

public class PlayerInteractions : MonoBehaviour {

[SerializeField] private GameObject chestHelperMessage;

private void OnCollisionEnter2D(Collision2D collision) {
    if (collision.gameObject.CompareTag("Chest")) {
        chestHelperMessage.SetActive(true);
        if (Input.GetKeyDown(KeyCode.E)) {
            Debug.Log("You got a weapon");
        }
    }
}

private void OnCollisionExit2D(Collision2D collision) {
    if (collision.gameObject.CompareTag("Chest")) {
        chestHelperMessage.SetActive(false);
    }
}

private void OnCollisionStay2D(Collision2D collision) {
    if (collision.gameObject.CompareTag("Chest")) {
        if (Input.GetKeyDown(KeyCode.E)) {
            Debug.Log("Chest opened");
        }
    }
}

}

none of the function is giving inputs. Please help. Thanks

CodePudding user response:

for OnCollisonEnter2D and OnCollisionExit it is pretty unlikely that you press the key down exactly in the very same frame where the collision event happens.

Also for OnCollisionStay2D the call happens together with FixedUpdate, the physics routine. They are not happening every frame so it can happen that also here you miss the one or other input.

In general you should get user input in Update and do e.g.

// In case you need the chest reference itself
private GameObject currentChest;

IEnumerator CheckInputRoutine()
{
    if (chestHelperMessage.activeSelf && Input.GetKeyDown(KeyCode.E))
    // Or also
    //if(currentChest && Input.GetKeyDown(KeyCode.E)) 
    {
        Debug.Log("You got a weapon");
    }
}

private void OnCollisionEnter2D(Collision2D collision) 
{
    if (collision.gameObject.CompareTag("Chest")) 
    {
        chestHelperMessage.SetActive(true);  
        currentChest = collision.gameObject;

        StartCorouine (CheckInputRoutine());  
    }
}

private void OnCollisionExit2D(Collision2D collision) 
{
    if (collision.gameObject.CompareTag("Chest")) 
    {
        chestHelperMessage.SetActive(false);
        currentChest = null;
        StopAllCoroutines ();
    }
}

CodePudding user response:

Thanks @RoberStan, the code given by you is not working for my game, but some changes made it work.

Here is the new code

[SerializeField] private GameObject chestHelperMessage;

private GameObject currentChest;

private void CheckInput() {
    if (chestHelperMessage.activeSelf && Input.GetKeyDown(KeyCode.E)) {
        Debug.Log("You got a weapon");
    }
}

private void OnCollisionEnter2D(Collision2D collision) {
    if (collision.gameObject.CompareTag("Chest")) {
        chestHelperMessage.SetActive(true);
        currentChest = collision.gameObject;
    }
}

private void OnCollisionExit2D(Collision2D collision) {
    if (collision.gameObject.CompareTag("Chest")) {
        chestHelperMessage.SetActive(false);
        currentChest = null;
    }
}

private void Update() {
    CheckKeyPress();
}

private void CheckKeyPress() {
    if (chestHelperMessage.activeSelf) {
        CheckInput();
    }
}
  • Related