- Home /
Giving a function time duration
Hi i'm know the little standarts of unity, but I have a difficulty with giving a function a duration. With this script I want to reveal a texture onscreen when I hit a target. But I have this problem on simmilar cases where I want to give a function a time duration. Sorry for my bad english. I hope the script shows what i want to have. If someone knows it, it helps me a lot with other pieces of the game. Lots of thanks in advance!
var collision: boolean = true;
var recovertime = 5;
function Update() {
var recover = recovertime - Time.time;
if (recover <= 0) { collision = true; } }
function OnControllerColliderHit (hit : ControllerColliderHit) {
if(hit.gameObject.tag == "enemy" & collision) {
print("Au!");
Script_Lives.LIVES -= 1;
collision = false;
recovertime = 5 + Time.time;
var texture = GameObject.Find("Test");
var script2 = texture.GetComponent("Script_ouch");
script2.visible = true;
} }
Answer by flaviusxvii · Jun 21, 2011 at 08:00 PM
Choose a duration and then subtract Time.deltaTime on each call to Update(). Once you are down to zero then stop.
Easy peasy.
Answer by Corandy · Jun 21, 2011 at 08:20 PM
Oh I have made a terrible mistake, I thought that one was the easiest script to explain but with that script2.visible there is one what it makes difficult.
The one that maybe explain it truly is this one:
var boost: boolean = true; var boosttime = 5; var rentime = 2;
function Update () {
var recover = boosttime - Time.time; var rennen = rentime - Time.time;
if (recover <= 0) { boost = true; }
if (Input.GetKey (KeyCode.LeftShift)&& boost)
{ print ("Running!");
var Karakter = GameObject.Find("Character");
var script = Karakter.GetComponent("CharacterMotor");
script.movement.maxForwardSpeed = 15;
Camera.main.fieldOfView = 130;
rentime = 2 + Time.time;
if (rennen <= 0){
print ("Not Running!");
script.movement.maxForwardSpeed = 10;
Camera.main.fieldOfView = 60;
boost = false;
boostime = 5 + Time.time;
}
}
}
Yes here I know what goes wrong, The timer to make the function happen works and that it triggers again after few seconds works. One thing that don't works is the duration of that function. I didn't understand exactly what your saying, but what do I wrong here? So sorry about that other question...
Let me explain, the boosttime triggers the event. when the event started it takes 2 seconds before it goes to the next event where he reset some stats. Is this the wrong way how I doing it or are there little mistakes?
"var $$anonymous$$arakter"
Uhhghghghh.. You shouldn't name your variables with upper case letters at the beginning. Reserve upper case for Classes (and possibly methods but I don't like that either).
Oh.. and this was probably an inappropriate "answer" to your question.
No your right, that was not good grammar for program$$anonymous$$g, I have fixed that.