- Home /
damage script
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.
Answer by fafase · Apr 07, 2012 at 03:34 PM
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;
}
im getting compiler error unknown identifier playerscript and damage
As I said you need to chnage them to your settings What is the name of your script where the health is? And is the health variable static?
my health script name is healthbar.cs, i dnt think it is static though
oh you use c#, my script is in Js. And unfortunately I don't use c#...
are there any scripts that you know that i can use as a healthbar tht will work with tht