I have a group of blocks that are supposed to vanish when the player collides with them. For this, I use OnTriggerEnter, and it works just fine. The issue comes when the player leaves the collision box of the block. 95% of the time, the OnTriggerExit runs and the block re-appears. For some reason, 5% of the time the blocks will not re-appear.
I’m just curious to see if anybody has any ideas. It’s almost as if OnTriggerExit gets confused when it’s called by multiple objects in quick succession.
The blocks are tagged as platforms and the player is tagged as player. Also, I’m using discrete collision and using a fixed timestep of 0.02.
Here is my vanishBlock Script:
#pragma strict
private var animPlaying = false;
private var delayedEnable = false;
private var isIn = false;
function Start () {
gameObject.GetComponent(AnimatedSpritesheet).columns = 8;
gameObject.GetComponent(AnimatedSpritesheet).framesPerSecond = 0;
gameObject.GetComponent(AnimatedSpritesheet).repeat = false;
gameObject.GetComponent(AnimatedSpritesheet).SetFrameOrder([0]);
}
function Update () {
if (delayedEnable){
if (!isIn){
EnableCollider();
delayedEnable = false;
}
}
}
function PlayAnimation () {
if (!animPlaying){
DropCollider();
animPlaying = true;
gameObject.GetComponent(AnimatedSpritesheet).columns = 8;
gameObject.GetComponent(AnimatedSpritesheet).framesPerSecond = 8;
gameObject.GetComponent(AnimatedSpritesheet).repeat = false;
gameObject.GetComponent(AnimatedSpritesheet).SetFrameOrder([0,1,2,3,4,5,6,7]);
}
}
function PlayReverseAnim (){
gameObject.GetComponent(AnimatedSpritesheet).columns = 8;
gameObject.GetComponent(AnimatedSpritesheet).framesPerSecond = 8;
gameObject.GetComponent(AnimatedSpritesheet).repeat = false;
gameObject.GetComponent(AnimatedSpritesheet).SetFrameOrder([7,6,5,4,3,2,1,0]);
}
function DropCollider (){
yield WaitForSeconds (1.0);
collider.enabled = false;
yield WaitForSeconds (1.0);
if (!isIn){
EnableCollider();
}
else {
delayedEnable = true;
}
}
function EnableCollider (){
if (!isIn) {
PlayReverseAnim();
collider.enabled = true;
}
yield WaitForSeconds (1.0);
animPlaying = false;
}
function isInYes(){
isIn = true;
}
function isInNo(){
isIn = false;
}
Here is my vanishBlockCollider Script:
` #pragma strict function OnTriggerEnter (other : Collider){ if (other.CompareTag("Player")){ gameObject.SendMessageUpwards("PlayAnimation"); gameObject.SendMessageUpwards("isInYes"); } } function OnTriggerExit (other : Collider){ if (other.CompareTag("Player")) gameObject.SendMessageUpwards("isInNo"); } `