- Home /
How do i make my enemies do damage?
Hi, i'm making a zombie game and i already have the spawners completed and the zombies follow me but i have no idea how to make them hurt the player. The player life is made and i can kill the zombies i just need them to be able to kill me. Any help is appreciated! thx.
Answer by Tpaley · Mar 24, 2013 at 06:07 PM
This is javascript.
maybe have something like this for the player:
var waitTime : int; //# of seconds to wait before they can hurt you again.
var immune : boolean = false; //DO NOT EDIT
var timeExisted : int; //DO NOT EDIT
function Awake () {
if (Time.time > waitTime + timeExisted){
immune = 0;
}
}
function OnCollisionEnter ( c : collider ) {
if (c.gameObject == "Zombie" && immune == 0) {
playerHp = playerHp - 5;
timeExisted = Time.time;
immune = 1;
}
}
Hope that helps!
so do i only put this on the player? dont have to add anything to zombies?
ok so i put in the variables as such var waitTime : int = 0.5; var immune : boolean = false; var timeExisted : int;
function Awake () { if (Time.time > waitTime + timeExisted){ immune = 0.5; } }
function OnCollisionEnter ( c : SphereCollider ) { if (c.gameObject == "Zombie" && immune == 0.5) { playerHp = playerHp - 10; timeExisted = Time.time; immune = 0.5; } }
And got these errorsAssets/PlayerTakeDamage.js(15,18): BCE0022: Cannot convert 'float' to 'boolean'. Assets/PlayerTakeDamage.js(13,20): BCE0005: $$anonymous$$ identifier: 'playerHp'. Assets/PlayerTakeDamage.js(7,18): BCE0022: Cannot convert 'float' to 'boolean'.
you just need to do collider, not SphereCollider.
Error: $$anonymous$$ identifier: 'playerHp'.
fix: playerHp is the health of the character. You said that you already had the health there. I was just referencing to a supposed variable name. you need to define the health somewhere else in the script. Error: Cannot convert 'float' to 'boolean'. (both of the errors relating to this)
Fix: for the setting of immune I wasn't thinking and I used set to 0 and set to 1 ins$$anonymous$$d of true and false. replace the immune = 0; with immune = false; and replace immune = 1; with immune = true;
That should work.
and nothing on the zombies except for chase scripts.
Thought i had it but these errors appeared
Assets/PlayerTakeDamage.js(15,18): BCE0022: Cannot convert 'float' to 'boolean'.
Assets/PlayerTakeDamage.js(13,43): BCE0051: Operator '-' cannot be used with a left hand side of type 'System.Type' and a right hand side of type 'int'.
Thanks for all the help this part is out of the field of what I know.
Answer by Frosty96 · Mar 24, 2013 at 08:11 PM
acutally that comment ended odd here is what i did
var waitTime : int = 0.5;
var immune : boolean = false;
var timeExisted : int;
function Awake () {
if (Time.time > waitTime + timeExisted){
immune = 0.5;
}
}
function OnCollisionEnter ( c : SphereCollider ) {
if (c.gameObject == "Zombie" && immune == 0.5) {
playerHp = playerHp - 10;
timeExisted = Time.time;
immune = 0.5;
}
}
And i got these errors Assets/PlayerTakeDamage.js(7,18): BCE0022: Cannot convert 'float' to 'boolean'. Assets/PlayerTakeDamage.js(13,20): BCE0005: Unknown identifier: 'playerHp'. Assets/PlayerTakeDamage.js(15,18): BCE0022: Cannot convert 'float' to 'boolean'.
This is my complete fix. This should remove all the errors.
var waitTime : int = 0.5;
var immune : boolean = false;
var timeExisted : int;
var playerHp = 10; //or whatever you want.
function Update () {
if (Time.time > waitTime + timeExisted){
immune = false;
}
}
function OnCollisionEnter ( c : Collider ) {
if (c.gameObject == "Zombie" && immune == false) {
playerHp = playerHp - 10;
timeExisted = Time.time;
immune = true;
}
}
Hope this works. make no edits. tell me it it works.
It works but how do I get my zombies to damage and kill him? Such as set the damage the zombies do. Something like a zombie can attack once per second, it does 10 damage, and its range is 1?
well you should probably either lower the damage or increase the health. I guess you make the OnCollisionEnter script look like this: (you can do edits)
function OnCollisionEnter ( c : Collider ) {
if (c.gameObject == "Zombie" && immune == false) {
playerHp = playerHp - 10;
if (playerHp <= 0){
Application.LoadLevel(LoadedLevel); //this is for restarting the level. you can also do Destroy(GameObject); to kill the player but then it glitches with the main camera so you need to do stuff there. if you want a death screen then just do Application.LoadLevel ("NA$$anonymous$$E OF SCENE/LEVEL"); Your choice.
}
timeExisted = Time.time;
immune = true;
}
}
Answer by Alpha_Guac · Oct 19, 2015 at 10:40 AM
try this player has this function void Damage(int damageDealt) { int Health-= damagedealt } Zombie has this Function int damage void OnCollisionEnter(Collider) { if (collider.gameObject == "player) SendMessage("Damage",damage,RecieverNotRequired) }
check here for further details http://docs.unity3d.com/ScriptReference/Component.SendMessage.html
Your answer
Follow this Question
Related Questions
Scrolling space shooter - making enemies drop currency 1 Answer
How to make enemy shoot at player 0 Answers
why isn't my enemy shooting at me? 0 Answers
Bullet Interaction 2 Answers