- Home /
Rate of health loss in trigger box.
so i have a script that makes it when i go in the box area i loos 10 hp points but i dont know how to make it happen every second longer again if i dont leave the box. i can't figure out how to make my Time script part.
var Damage = 10; var Rate = 1;
function OnTriggerEnter (col : Collider) { var player : CharacterDamage = col.GetComponent(CharacterDamage);
player.ApplyDamage(Damage);
}
function Reset () { if (collider == null)
gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }
Answer by KeithK · Apr 03, 2011 at 06:54 AM
You could do the intial damage like you're doing now. Then for the timed damage that occurs while you stay in the box try this.
It uses the OnTriggerStay event, which is fired continuously after OnTriggerEnter, and until OnTriggerExit. You keep track of the total time in RateCount by adding the delta time value of Time until it equals or exceeds the Rate you want, apply the damange, reset the count.
var Damage = 10; var Rate : float = 1.0f; var RateCount : float = 0.0f;
function OnTriggerEnter (col : Collider) { var player : CharacterDamage = col.GetComponent(CharacterDamage);
player.ApplyDamage(Damage);
RateCount = 0;
}
function OnTriggerStay (col : Collider) { RateCount += Time.deltaTime;
if (RateCount >= Rate){ var player : CharacterDamage = col.GetComponent(CharacterDamage); player.ApplyDamage(Damage);
RateCount = 0.0f;
print("Fire!");
} }
Open up your console in Unity Editor and see if you get that print("Fire") appearing.
Hope this helps. :)
At the part where it says "RateCount += Timer.deltaTime;".... i am no expert at scripting but is it a typo, should it really be Time, no Timer. but even that way the script is not working and i will work on this hard but i could use any suggestions you might know
Ah sorry, I'll fix that. It should indeed be Time and not Timer. Hmm that is strange, the timed damage should be firing correctly. I'm not JavaScript guru though, I write my scripts in C#. So it may well be something funny in my JS syntax that's not working. I'll change the answer code, and you can give that a try.
The only thing I'm certain of here is the logic that makes the code in the OnTriggerStay if statement fire once every Rate. Stick a print() in the if statement and check me on that though.
I think i could figure this out if new what Time.deltaTime is, i kind of know what it is but i can't implement it into any if my scripts cause i don't know how to use it and i THIN$$anonymous$$ we could use it some where in the (RateCount >= Rate) but im not sure how that would work..... maybe not?
Time.delaTime is the time that has passed since the last frame. Tell me what there is to figure out in this script above? What isn't working?
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
OnTriggerStay Problem with rate? 1 Answer
Does a mesh collider animate? 0 Answers
How to create an Idle counter ? 2 Answers
Collision, tags, and triggers 1 Answer