Countdown when out of boundry

I am trying to created a countdown to end the game if someone stays out of bounds. Right now the countdown is in the update function. I don’t think this is correct. At the moment it doesn’t work at all, what am I doing wrong?

`
var mastercam : Transform;
var maxX = 0;
var minX = 0;
var maxZ = 0;
var minZ = 0;
var maxY = 0;
var minY = 0;
var outOfBounds : boolean = false;
var BufferTime = 500;
var timeleft : int;
var onetime = false;

    function Update () {
    	if (mastercam.position.x > maxX || mastercam.position.x < minX || mastercam.position.z > maxZ || mastercam.position.z < minZ || mastercam.position.y > maxY || mastercam.position.y < minY)
    	{
    		if (onetime == false)
    		{
    			EndTime();
    			var endTime = Time.time + BufferTime;
    			onetime = true;
    			print("onetime");
    		}
    		if (onetime == true)
    		{
    			timeleft = endTime - Time.time;
    		}
    		if (timeleft <= 0)
    			{
    			GameOver.OutOfBounds = true;
    			}
    		print("out of bounds");
    		outOfBounds = true;
            }
    	else
    		outOfBounds = false;
        }

`

Thanks!

Not sure what’s the whole “onetime” thing about and I assume BufferTime is your time limit to be outside the bounds, correct me if I’m wrong but can’t you just do this

Change your timeLeft variable to float

var timeleft : float;

function Update () 
{
    if (mastercam.position.x > maxX || mastercam.position.x < minX || mastercam.position.z > maxZ || mastercam.position.z < minZ || mastercam.position.y > maxY || mastercam.position.y < minY)
    {
        if (timeLeft < BufferTime)
        {
            timeLeft += Time.deltaTime;
            if(timeLeft >= BufferTime)
            {
                GameOver.OutOfBounds = true;
            }
        }
        print("out of bounds");
        outOfBounds = true;
    }
    else
    {
        outOfBounds = false;
    }
}

Beyond that if you wanna display a timer, you could either check the timeleft and display it everytime its Ceiled or Floored value changes meaning it’s a new second outside the bounds.

Another method could be to trigger a yield coroutine that works every second and you interrupt that coroutine when the player is not out of bounds.

See coroutines doc
Here

You declared the variable endTime inside the if block so you can’t use it outside. It should be declared outside of all blocks.

The onetime thing is a way how you can do it but i would prefer this way :wink:

var mastercam : Transform;
var max : Vector3;
var min : Vector3;
var BufferTime = 500.0;

var outOfBounds = false;
var endTime : float; // The time normally a float
var timeleft : int; // here's ok if you want to view only the seconds


function Update () {
    outOfBounds = mastercam.position.x > max.x || mastercam.position.x < min.x || mastercam.position.z > max.z || mastercam.position.z < min.z || mastercam.position.y > max.y || mastercam.position.y < min.y;
    if (!outOfBounds)
    {
        endTime = Time.time + BufferTime;
    }
    timeleft = endTime - Time.time;
    if (outOfBounds && timeleft <= 0)
    {
        GameOver.OutOfBounds = true;
    }
}