I have two gameobjects.
One is a Player tagged “Player”. With a rigidbody, a capsule collider 2D and everything.
The other one is a button with a capsule collider 2D and a box collider 2D labeled “Is Trigger”.
When I’m inside the trigger and press “E”, it sometimes works and sometimes doesn’t. It detects the trigger, because I have set a bool to know it.
What am I missing?
public class HubButtonScript : MonoBehaviour
{
public SpriteRenderer SpriteRen;
public Sprite Button2;
public bool isInside = false;
// Start is called before the first frame update
void Start()
{
SpriteRen = GetComponent<SpriteRenderer>();
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
isInside = true;
if (Input.GetKey(KeyCode.E))
{
PressButton();
Debug.Log("Button Pressed");
}
}
}
private void PressButton ()
{
SpriteRen.sprite = Button2;
}
}