How to add delay to spwantime?

Ok I have managed to create this simple respawn script that makes the player respawn after falling from a certain hight:

var reSpawnPoint: Transform;

function Update ()
{
if(gameObject.transform.position.y < -8.512409)
gameObject.transform.position = reSpawnPoint.position;

}

But I want the player respawn to be delayed around 5 seconds after the certain y coordinate has been passed. Is there any smart way to do this?

Hi,

var reSpawnPoint: Transform; 
var timer : float = 0.0;
function Update () 
{ 
if(gameObject.transform.position.y < -8.512409){
timer += Time.deltaTime; 
if(timer>5)
gameObject.transform.position = reSpawnPoint.position; 
}
}

have fun :lol:

Don’t forget to put timer back to 0.

timer = 0;

Thanks a bunch!

Is there also possible to make the repawn happen after the left mouse button is pressed?
Just to try out what works best. :stuck_out_tongue:

There are functions called OnMouseUp () and OnMouseDown () - they are run when the mouse button is pushed down or released. Just put the respawn command in one of those :smile:

Thanks guys! This helped :stuck_out_tongue:

But how do I make this happen over and over?
Right now the timer only works for the first time. After respawning again and doing the same action, the timer does not work for the second time and the character respawns immediately.

Be sure to read AkilaeTribe’s post.

Forgive me for being totally **tard, but do you mean that I should add “timer = 0;” to the script? If so where?

Yes, you should add that line to the script.

As for where, try thinking through it logically. Here’s the relevant code, formatted for easier reading:

if (gameObject.transform.position.y < -8.512409 {
    timer += Time.deltaTime;
    if (timer > 5) {
        gameObject.transform.position = respawnPoint.position;
    }
}

If we were to write that out ‘in words’, it might look something this:

if y is below the 'respawn threshold'
    increase timer
    if timer has expired
        respawn player
    end if
end if

So based on that, where would it make sense to reset the ‘timer’ variable to zero? (Fortunately it’s a short script, so there are only a few places it can possibly go :slight_smile:

Sould it look something like this maybe? :S

if (gameObject.transform.position.y < -8.512409 { 
    timer += Time.deltaTime; 
    if (timer > 5) { 
        gameObject.transform.position = respawnPoint.position; 
    timer = 0;
   } 
}

Feel like a complete idiot. :S
Just started my internship three weeks ago and has been forced to learn scripting for the first time. Only worked with asset production and visual design previously, but really appreciate your effort to try and make me understand how it works. :stuck_out_tongue:

You colud simply put it like this

if (gameObject.transform.position.y < -8.512409)
{
yield WaitForSeconds(5);
gameObject.transform.position = reSpawnPoint.position; 
}

this will always delay the script for 5 seconds when the player falls below the height specified. the timer never needs reseting i dont know why you are being encouraged to add more processes in your script

yield

stops the function

WaitForSeconds(5);

waits for seconds to pass from the yield comand
in this case it is 5 seconds before executing the next action.

i think you can figure out that if you change that 5 to a 2 then it will wait for 2 seconds.

should do the job

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

You can’t use Yield in Update (), FixedUpdate ().

Question : when I ask questions on Unity forum, very few people / no one answers me. Must I understand that no one read my solutions too ? Pretty mind-blowing…

my apologies and no disrespect intended to anybody else.

I stand corrected :sweat_smile: and i learned something new.

That looks right to me :slight_smile:

Does it seem to be working correctly?

I’ll try it first thing in the morning when I get to the office. :wink:

Yeah, that script should work perfectly for you.

In other news, that “Binary Sudoku” thing is hilarious.

When I added the “timer = 0;” part nothing happened when with the player. When I say nothing happened I mean NOTHING happened. The player did not respawn. Tried to change it to “timer = 5;” and even 25, what happened then is that the player respawned instantly instead of after a five second delay.

Can you post your current code?

var reSpawnPoint: Transform; 
var timer : float = 0.0;

function Update () 

{
	if(gameObject.transform.position.y < -3){ 
	timer += Time.deltaTime; 
	if(timer>5)
	gameObject.transform.position = reSpawnPoint.position;
	timer = 0;
} 
}

I don’t use Unity js, but I’m assuming that (like C and C++) it’s technically legal to omit the brackets following a conditional.

In C and C++, this practice is (generally) highly discouraged, as it leaves the door open for errors just like the one in your code. If we go ahead and add in some clarifying brackets and indentation, we get:

if (gameObject.transform.position.y < -3) { 
    timer += Time.deltaTime; 
    if (timer > 5) {
        gameObject.transform.position = reSpawnPoint.position;
    }
    timer = 0; 
}

This is the classic ‘omitted brackets’ error; although the line ‘timer = 0’ is meant to be associated with the conditional ‘if (timer > 5)’, it’s actually executed whenever the object’s y position is below the threshold.

To fix this, add brackets after the ‘if (timer > 5)’ conditional, and make sure the line ‘timer = 0’ is inside the brackets. (This is actually how you had it earlier, but you must have removed the brackets at some point.)