- Home /
how to make enemies have health and get damaged by bullets
i need a code that will make my enemies get damaged when they are hit by my bullets. I have not put health or anything on the yet apart from a collider.
what type of bullets are you using in your gun RayCast or instantiated bullets
it will not destroy the object. i added the Destroy script and has not worked i have correctly placed the codes and tagged the objects. any help? thanks
Hey. I know I am a bit late but I did everything right and I got no errors but the collider doesn't do anything. srry for bad english btw.(If it's bad)
Answer by deltamish · Nov 02, 2012 at 11:13 AM
Hi, May i suggest you to use raycastgun because it is way better and you can add penetration too
Heres is the damage script
var health:float = 90;
function Damage(dmg:float){
health -= dmg;
}
function Update (){
if(health <=0){
//add your death code
}
}
Add this code to your bullet
var dmg:float = 20;
function OnTriggerEnter(hit:Collider){
if(hit.tag == "Enemy"){
hit.transform.SendMessage("Damage",dmg);
}
}
assign the tag Enemy to the object you added the Damage script
thanks now how would i make the entity i shoot go ragdoll
what death code should i use? what is the death code? can you post one?
@deltamish I tried this, but I get the error : Send$$anonymous$$essage BulletDamage has no receiver!
All I did was change the variable names, I changed em back and same error
Thanks a million! Deltamish and jrocker2002 your the best!
Answer by skiedude · Aug 23, 2016 at 06:26 AM
I'd also look into https://unity3d.com/learn/tutorials/projects/survival-shooter-tutorial its a follow along tutorial by Unity a couple years ago (check the attached PDF in the assets for specific unity 5 updates) and it goes over setting playerhealth/enemy health and taking damage.
Has some good starter scripts to look at as well.
Thanks! You have helped an 11 year old learn to script (me). I have been using unity for 4 years now ever since I was 7 and it's awesome. It's people like you who always help me learn more!
Wow! You speak and act so maturely. I hope you keep at what you're doing!
Answer by Jrocker2002 · Mar 03, 2014 at 03:58 AM
To do a death code use this
if(health <= 0) { //Play a death anim or
}
BEST HEALTH AND DA$$anonymous$$AGE SCRIPT I HAVE EVER SEEN THAN$$anonymous$$S deltamish AND Jrocker2002!!!!!!
Your answer
Follow this Question
Related Questions
Bullets kill enemy... but continue to travel? 1 Answer
Bullet not destroying on collision 1 Answer
A node in a childnode? 1 Answer
spraying bullets 3 Answers
Preventing Rapid Fire 2 Answers