I’m trying to make a switch in my top-down RPG and I want to be able to allow the player to push an object onto the switch which will open a door but I don’t want the player or the object they’re pushing to collide with the switch.
The object the player is pushing has the tag “binbag” and in using OnCollisionEnter2D to detect that the binbag is on top of the switch.
Here is the code I’m using for collision detection:
void OnCollisionEnter2D(Collision2D obj)
{
if (obj.gameObject.tag == "binbag")
{
animDoor.SetBool ("SwitchIsOn", true);
GetComponent<SpriteRenderer> ().sprite = switchOn;
}
}
void OnCollisionExit2D(Collision2D obj)
{
if (obj.gameObject.tag == "binbag")
{
animDoor.SetBool ("SwitchIsOn", false);
GetComponent<SpriteRenderer> ().sprite = switchOff;
}
}