- Home /
'Question' Answered.
Please, I need a damage script
Hi, I've been trying to make a script to damage my NPC's they already have hitpoints and health but I can't do any damage to them. If someone could be kind enought to paste one as an anwsere or reply it would help me alot. Thatk's for your time and good luck with unity!
Yes, I am quite new to unity.
Answer by Peter G · May 30, 2010 at 11:01 PM
See the FPS Tutorial for a detailed explanation.
A simple example.
The enemy script.
var damage = 10;
function OnCollisionEnter (col : Collision) { col.gameObject.BroadCastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); }
player script:
var health = 50
function ApplyDamage (damage : int) { health -= damage;
if(health <= 0) {
Die();
}
function Die () { //Die and or Respawn }
Thankyou very much, it's good to see experiance user help newer ones :)
if anyone eles has a alternative script please do post :)
im sorry i am a noob to scripting. where do i put the semicolon at
Answer by Brushy · Mar 31, 2013 at 09:29 PM
static var health : int = 0;
var collided : boolean = false;
var healthguitext : GUIText;
function Start(){
health = 10;
}
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.collider.gameObject.tag == "(name of object here)") {
LessDamage();
}
}
function Update(){
healthguitext.text = health.ToString();
}
function LessDamage(){
if(!collided) {
health--;
collided = true;
yield WaitForSeconds(1);
collided = false;
}
}
just replace the (name of object by the name of the object you want to apply damage to.
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Armour / Damage System RPG-like? 2 Answers
Bullet scripting , i need help 1 Answer
Rate of health loss in trigger box. 1 Answer
Destroy enemy using raycasts 2 Answers