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
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.
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
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.
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.
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…
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.
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:
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.)