Hello, I am new to scripting and I’ve written a bit of code that should serve as a respawn for the vehicle AI.
The script teleports successfully and properly detects whether in bounds or out, yet has some issues with the timing, it just seems to want to teleport when it wants.
I’ve attached my code below, would anyone help me figure out what might be going on and suggest ways to repair it?
Thanks in advance!
#pragma strict
var TelePosition : Transform; //The position the AI will return to
var canTeleport = false; //determines if AI is out of play area
var secondsToWait = 9; //seconds to wait before determining whether to teleport
var waited = false; //have the seconds to wait passed?
function OnTriggerExit(other: Collider) {
if (other.tag == "aiArea" ) //if the AI has left the play area
{
waited = false;//the ai just left, it has not waited yet
canTeleport = true;//since the ai is out of bounds, it is allowed to teleport
waitFirst();//wait and inform when ai has waited (wated will be set to true)
if (canTeleport && waited )//the ai is out of bounds and has waited
//(if can teleport has since changed, this won't run since it is no longer needed.)
{
transform.position = TelePosition.position;//respawn the ai with specific rot/pos
transform.rotation = TelePosition.rotation;
waited = false; //the ai is no longer out of bounds, it is not waiting
canTeleport = false; //the ai is in bounds, it has no need to teleport
}
}
}
//if at any time the ai returns in bounds,
//remove teleport abilities (overiding priviledge from above)
function OnTriggerEnter(other: Collider) {
if (other.tag == "aiArea" ) { //the ai has returned to the playing area
canTeleport = false; //since the ai has returned, there is no need to teleport
}
}
function waitFirst()
{
yield WaitForSeconds (secondsToWait); //waits for specified amount of time
waited = true; //the ai has waited
}