in my game there is fire spread out throughout the map. I cant find out how to make a script that causes little repeating damage to the players health when he steps on the fire.
Put a box collider on the fire, set it to IsTrigger and child it to the fire. The add that script to the box:
function OnTriggerStay(other:Collider)
{
if(other.gameObject.tag=="Player")
healthbar.health-=damage;
}
This is considering that your player health is a static variable (which is fine in this case) in a PlayerScript.js
Change as you need.
Be careful that this is called every frame, so you might want to temper the damage with some kind of timed damage. In this case, you would have to think slightly differently.
private isIn:boolean;
vat timing:int;
function Start(){
isIn = false;
}
function Update(){
if(isIn){
timing = Time.time;
if(timing%2)
healthbar.health-= damage;
}}
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag == "Player")
isIn =true;
}
function OnTriggerExit(other:Collider){
if(other.gameObject.tag == "Player")
isIn =false;
}