How to disable a boxcollider2d temporary?
hi guys,
So i'm making a game similair to flappyBird and whenever you trough the pipes you get a point. The problem is sometimes my player hits the box twice. it gets two piont instead of one. So i want to disable my boxcollder2d for a second after it gets hit by my player. The boxcollider2d are in a loop so they can't be disabled forever.
code i used for my collider2d to trigger:
void OnTriggerEnter2D(Collider2D collider) {
if(collider.tag=="Player"){
PlaySound(0);
Scores.AddPoint();
}
}
get trigger collider with GetComponent( ). you can disable any component by setting Component.enabled to false.
thanks for the reply
this disables my boxcollider2D after it is triggerd. my boxcolliders2d are in a loop so this is not a option. can't i set a timer or something?
Answer by dreamhex · Dec 29, 2015 at 12:10 PM
You could try this: http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
So it's like:
void OnTriggerEnter2D(Collider2D collider) {
if(collider.tag=="Player"){
PlaySound(0);
Scores.AddPoint();
GetComponent<BoxCollider2D> ().enabled = false;
StartCoroutine(EnableBox(1.0F));
}
IEnumerator EnableBox(float waitTime) {
yield return new WaitForSeconds(waitTime);
GetComponent<BoxCollider2D> ().enabled = true;
}
I feel like there is a better option to handle this situation, maybe someone will tell you, but this should work too.
Does it work? Or did you go with LazyElephant's solution? That should work too with OnTriggerExit2D function.
Your solution worked . I dont know how to do it with the OnTriggerExit2D function.
Your answer
Follow this Question
Related Questions
How to let player hit a boxcollider just once?(2D) 1 Answer
wanna find size datas from boxcollider2D in script 0 Answers
GameObject keeps colliding for sometime after being destroyed 1 Answer
System Equipment and Changing Clothes in 2d Endless Runner Game ? 0 Answers
i want random color in my five block but not same color how i do that 3 Answers