Reputation: 13
I am trying to have my player walk onto a GameObject and if they are on that object and they press the space key show a debug log.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Level_1" && Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Both Conditions Reached");
}
}
Upvotes: 0
Views: 70
Reputation: 71
This would only trigger if they were holding down the space bar as they entered the object. You would do better to check if they were currently colliding with the object when the spacebar is pressed. (or do the check for both in Update)
Call this in your player object:
private void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Level_1") {
player.isInside = true;
}
}
private void OnTriggerExit2D(Collider2D other) {
if (other.gameObject.tag == "Level_1") {
player.isInside = false;
}
}
And use this to check for the spacebar:
public void Update() {
if (Input.GetKeyDown(KeyCode.Space) && player.isInside == true) {
Debug.Log("Both Conditions Reached");
}
}
Upvotes: 1