- Home /
Damage/Healing over time
After referencing numerous relevant scripts in a attempt to Frankestein my own to life I have hit a wall. My intention is to have the player affected by the health modifiers over time instead of an instance as it currently set. I believe it requires OnTriggerEnter and OnTriggerExit and possibly something to do with Time.deltatime?
Just starting out with Unity programming so please forgive my ignorance any help or insight would be greatly appreciated.
var playerHealth = 100;
function Update () {
if(playerHealth <= 0){
playerHealth = 0;
killPlayer();
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.CompareTag("Black Orbit"))
{
playerHealth -= 2;
}
if (other.gameObject.CompareTag("White Orb"))
{
playerHealth += 1;
}}
Answer by Wakeful · Jul 20, 2014 at 12:31 PM
I normally use c# but try :
{
playerhealth = playerhealth - Time.deltaTime;
}
And switch OnTriggerEnter
to OntriggerStay
Thats if u only want it to subtract health when on contact with that object. If you want it to constantly lose health:
if(other.gameObject.CompareTag("Black Orb"))
{
damaging = true;
}
if(damaging == true)
{
playerhealth = playerhealth - Time.deltaTime;
}
Try that! Sorry if this doe not work, I'm on a mobile right now and can't check.
Simply changing trigger to OnTriggerStay solved the basic mechanics of my intended function. Now I would like to modify the rate at which it increases or decreases. I tried using your code snippet:
playerhealth = playerhealth - time.deltatime;
In both the update and trigger functions, it reported "$$anonymous$$ identifier: 'time'." So I tried adding:
var time = #
where # is a random number I imputed but that doesn't seem to effect the rate.
I really appreciate yourself taking the time to $$anonymous$$ch me some things and will be happy to pay it forward as soon as I can!
Just a typo : Time.deltaTime
it is Time with a capital T. If you use that number value and try playerhealth -= Time.deltatime * yourNumber;
that should have the desired effect!
Typos fixed. To change the rate of damage simply multiply Time.deltaTime by your damage per second.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Health Question 1 Answer
Damage script 0 Answers
The enemy don´t lose health, but why??? 0 Answers
Looking for skilled individual to assist with problem 1 Answer