- Home /
How to make a player health script and enemy damage script that is easily changed?
I need to have a health script that can access variable of a damage script that has public variable that can be changed easily for use on more than one monster. If someone can write this for me or give me some pointers I'd be very thankful.
Answer by Fabkins · Jan 17, 2014 at 01:01 AM
If its the health of the monsters rather than the main player, I assign a generic "damage" script that looks something like this to all monsters:
var healthPoints: float = 1000;
function ReceiveDamage ( amount )
{
healthPoints -= amount;
//Debug.Log("Recieved this amount of damage "+amount.ToString()+" now health="+healthPoints.ToString() );
if( healthPoints <= 0)
{
//Debug.Log("Destroy me");
ProcessDeath();
}
}
function ProcessDeath()
{
//Debug.Log("Process Death");
Destroy(gameObject);
}
If that enemy needs to die in a special way I would subclass and create a special script with something like:
class SpecialDamage extends Damage
{
function ProcessDeath()
{
// Process are really cool and special death
Destroy(gameObject,0.3);
}
}
To my bullets or laser or whatever it is that is going to harm these I then do:
var damageScript = hit.collider.gameObject.GetComponent(Damage);
if( damageScript)
{
damageScript.ReceiveDamage( bulletDamage );
}
You could use this on the main player also.
First I wanted to say something about necroposting... Then I saw your name.
Answer by getyour411 · Jan 16, 2014 at 02:28 AM
On your playerHealth script (probably attached to player) write a method like
public void AdjustCurrentHealth(int modHealth) {
curHealth -= modHealth;
if(curHealth<=0)
// do death code
}
On your monster script you would have something that sets the reference to the GameObject containing playerHealth script; example
phealth=GameObject.FindObjectWithTag("Player").GetComponent<playerHealth>();
then figure your dmg based on whatever your inputs are, monster level, weapon dmg modifiers, assign that to dmg var; finally
phealth.AdjustCurrentHealth(dmg);
Your answer
Follow this Question
Related Questions
Health below zero but shows negative numbers 1 Answer
Object Not Recieving Damage 3 Answers
Expecting ) found = 1 Answer
Enemy Health Damage 2 Answers
How to make fall damage using this script right here. 1 Answer