Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Frosty96 · Mar 24, 2013 at 08:20 AM · enemy damage

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frosty96 · Mar 24, 2013 at 06:12 PM 0
Share

so do i only put this on the player? dont have to add anything to zombies?

avatar image Frosty96 · Mar 24, 2013 at 06:19 PM 0
Share

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'.

avatar image Tpaley · Mar 24, 2013 at 06:36 PM 0
Share

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.

avatar image Frosty96 · Mar 24, 2013 at 07:05 PM 0
Share

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.

avatar image
0

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'.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tpaley · Mar 24, 2013 at 08:49 PM 0
Share

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.

avatar image Frosty96 · Mar 24, 2013 at 10:52 PM 0
Share

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?

avatar image Tpaley · Mar 24, 2013 at 11:17 PM 0
Share

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;
 }
 }
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scrolling space shooter - making enemies drop currency 1 Answer

How to make enemy shoot at player 0 Answers

I am making a FPS and I was wondering how i could make it so when the enemy gets close enough he starts to deal damage. 0 Answers

why isn't my enemy shooting at me? 0 Answers

Bullet Interaction 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges