- Home /
Question by
conka2000 · Jan 19, 2016 at 08:18 AM ·
collisiondestroy2d-platformercollision2ddestroygameobject
Check not colliding when object has been destroyed,Check if object no longer colliding if it's been deleted
I've been trying to make a button which when a shot prefab boulder (E_shoot) rolls onto it, opens a mechanism. However, I can't get the mechanism to close while the button no longer has the boulder on it as the onTriggerExit2d function does not work as the boulder becomes deleted while on the button, technically not leaving the collision but just not existing anymore. Anyone got any ideas how to make it work?
This is the code I'm trying at the moment but it results with the mechanism constantly playing one of the animations.
public class Button : MonoBehaviour {
public GameObject Mechanism;
bool mech_on = false;
bool colliding = true;
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.tag == "E_shoot" && !mech_on)
{
mech_on = true;
Mechanism.GetComponent<Animation>().Play("Move");
colliding = true;
}
}
void Update()
{
if (!colliding && mech_on)
{
Mechanism.GetComponent<Animation>().Play("Moveback");
mech_on = false;
}
colliding = false;
}
}
Comment