OnTriggerStay still working after OnTriggerExit

Hello,

The problem is after my player enters a specific region for 3 seconds he respawns. However, even after exiting and trying to reset the 3second timer variable, the trigger still counts to 3 and respawns when the player is no longer in the trigger.

var SecondsCount : int;

function OnTriggerStay () {
yield WaitForSeconds (1);
SecondsCount = 1;
yield WaitForSeconds (1);
SecondsCount = 2;
yield WaitForSeconds (1);
SecondsCount = 3;
IfStay();
}

function OnTriggerExit () {
SecondsCount = 0;
}

function IfStay () {
if (SecondsCount == 3)
RespawnPlayer();
}

(Cut out respawn code.)

I need to make the trigger ONLY work when the player is in the trigger, and for OnTriggerExit to apply it’s self when the player leaves the trigger before 3seconds.

I’ve also tried OnTriggerEnter, same results as OnTriggerStay. I’ve also tried adding a tag checker to IfStay function to only then execute the respawn player code, but that made no difference.

Cheers.

It seems to me that your OnTriggerStay code is not terminating and due to the nature of yield, will run to completion. Off the top of my head I would try using a timer and simply incrementing it by elapsed frame time and comparing to a set number.
If you get higher than that respawn. Do this without using yield WaitForSeconds and it should work.

Sorry. Time.deltaTime is what I mean.

You were right, yield was the culprit. I didn’t want to do any fancy timer stuff, figured it’d be the easiest to implement and how I was wrong.

But the following code was just as simple as well, silly me.

SecondsCounter += 1 * Time.time

In order to do that type of function, you would need to do a Coroutine.

this is your script corrected with a Coroutine.

var isIn : boolean=false;

function Start(){
	gameObject.StartCoroutine(myCoroutine());
}

function OnTriggerEnter (other : Collider) {
	if(other.gameObject.tag=="Player")
		isIn=true;
}

function OnTriggerExit (other : Collider) {
	if(other.gameObject.tag=="Player")
		isIn=false;
}

function myCoroutine(){
	var endsAt=0.0;
	while(true){
		if(isIn  endsAt==0.0)
			endsAt=Time.time + 3.0;
		if(isIn  endsAt > 0.0){
			if(Time.time > endsAt){
				RespawnPlayer();
			}
		}else{
			endsAt=0.0;
		}
	}
}

OK, there is still a concept error here. We are looking for a tag, and that’s not good. What happens when there is more than one player? Two player’s enter, one man leaves.

Lets look at it tracked by object instead.

var isIn : boolean=false;
var stopObject : GameObject;

function OnTriggerEnter (other : Collider) {
	gameObject.StartCoroutine(myCoroutine(other.gameObject));
}

function OnTriggerExit (other : Collider) {
	stopObject=other.gameObject;
}

function myCoroutine(object : GameObject){
	var endsAt=Time.time + 3.0;
	var cont=true;
	while(cont){
		if(stopObject==object){
			cont=false;
		}else{
			if(Time.time > endsAt){
				object.SendMessage("RespawnPlayer", null, SendMessageOptions.DontRequireReceiver);
				//RespawnPlayer();
				cont=false;
			}
		}
	}
}

With this method we just start a coroutine for each object that enters the trigger. (of course, if you just wanted player objects, you could still use the first example’s OnTriggerEnter check.

The coroutine would continue on for 3 seconds, or until the variable stopObject is equal to the object that it is checking. (set only when the object exits the field)

There is as still one problem that remains with this script. What happens when two objects leave the field on the same frame. Sadly, I am going to leave that up to you, if it ever happens.