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
1
Question by eeveelution8 · Mar 10, 2013 at 09:59 PM · aibullethealth

a Script for Enemy HP

ive been searching through tutorials, forums, and answers, and haven't found any good scripts.

the best one i found so far is this script,

 function ApplyDamage (damage : float) {
     // We already have less than 0 hitpoints, maybe we got killed already?
 var maxHealth = 100;
 var curHealth = 100;
 var Playerdamage1 = 8;
 var Playerdamage2 = 10;
 var Playerdamage3 = 15;
 var Playerdamage4 = 100;
 var Enemydamage = 10;
 var target : GameObject;
 var explosion : GameObject;
 var exp : GameObject;
  
 function Update(); {
     AddjustCurrentHealthEnemy(0);
 }
  
 function AddjustCurrentHealthEnemy(adjE) {
     curHealth += adjE;
  
     if(curHealth < 0)
         curHealth = 0;
  
     if (curHealth > maxHealth)
         curHealth = maxHealth;
  
     if(maxHealth < 1)
         maxHealth = 1;
  
     if(curHealth < 1) { //die
         var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
         var newExp:GameObject=Instantiate(exp,transform.position,transform.rotation);
         Destroy(gameObject);
     }
 }
  
 function OnCollisionEnter(collision : Collision){
     if(collision.gameObject.tag == "MG"){
         curHealth -= Playerdamage1;
     }
     
 function OnCollisionEnter(collision : Collision){
     if(collision.gameObject.tag == "PISTOL"){
         curHealth -= Playerdamage2;
     }
     
 function OnCollisionEnter(collision : Collision){
     if(collision.gameObject.tag == "SHOT"){
         curHealth -= Playerdamage3;
     }
     
 function OnCollisionEnter(collision : Collision){
     if(collision.gameObject.tag == "ROCKET"){
         curHealth -= Playerdamage4;
     }
     
 
  
     if(collision.gameObject.Find("Player")){
         target.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-Enemydamage);
         var newExplosion:GameObject=Instantiate(explosion,transform.position,transform.rotation);
         Destroy(gameObject);
     }
 }


but it doesnt work quite right. it asks at line 15 to replace the 'Update' with a (

and at line 16, it wants me to replace the ; with a :

but these break the script. whats wrong with it? also, if you know, i have many classes of bullets in the game. at lines 38-56, have the different bullet classes. if i did those right please inform me.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Kleptomaniac · Mar 10, 2013 at 11:51 PM

This script is kind of a mess. Here is it cleaned up and optimised:

 //Got rid of ApplyDamage() here, because it seemed to bear no purpose
 
 var maxHealth : int = 100; //Typed these, as dynamic typing could cost you precious execution speed
 var curHealth : int = 100;
 var playerDamage1 : int = 8; //Changed these to camelCase
 var playerDamage2 : int = 10;
 var playerDamage3 : int = 15;
 var playerDamage4 : int = 100;
 var enemyDamage : int = 10;
 var target : GameObject;
 var explosion : GameObject;
 var exp : GameObject;
  
 function Update() { //Got rid of ; here
     AddjustCurrentHealthEnemy(0);
 }
  
 function AddjustCurrentHealthEnemy(adjE) {
     curHealth += adjE; //I have no idea what adjE is, but I will leave it in there just in case (seems to be doing nothing)
  
     curHealth = Mathf.Clamp(curHealth, 0, maxHealth); //Simplified
     maxHealth = Mathf.Max(1, maxHealth);
  
     if(curHealth < 1) { //die
         var newExplosion : GameObject = Instantiate(explosion, transform.position, transform.rotation);
         var newExp: GameObject = Instantiate(exp, transform.position, transform.rotation);
         Destroy(gameObject);
     }
 }
  
 function OnCollisionEnter(collision : Collision) { //Don't need separate OnCollisionEnter functions
     if(collision.CompareTag("MG")){
         curHealth -= playerDamage1;
     } else if(collision.CompareTag("PISTOL")){
         curHealth -= playerDamage2;
     } else if(collision.CompareTag("SHOT")){
         curHealth -= playerDamage3;
     } else if(collision.CompareTag("ROCKET")){
         curHealth -= playerDamage4;
     } else if (collision.CompareTag("Player")) { //Make sure to tag player, "Player"
         collision.gameObject.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-enemyDamage);
         var newExplosion: GameObject = Instantiate(explosion, collision.gameObject.transform.position, collision.gameObject.transform.rotation);
         Destroy(collision.gameObject);
     }   
 }

I'm not sure if it will work, as I haven't tested. Also, there seemed to be a lot of ambiguous stuff in there, so I commented all the stuff I changed. :)

Hope that helps you,

Klep


EDIT:

Try it now. :)

Comment
Add comment · Show 8 · 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 eeveelution8 · Mar 10, 2013 at 11:58 PM 0
Share

i really appreciate the help, and im sorry for complaining, but i think it broke it. :I

it made many of the values unknown, i fixed most of them but what i don't know is how to fix the collision sensor

avatar image Kleptomaniac · Mar 11, 2013 at 12:03 AM 0
Share

Oh, whoops, yeah, just noticed some other errors and bits where I forgot to change the variable na$$anonymous$$g. Hang on I'll edit.

avatar image eeveelution8 · Mar 11, 2013 at 12:10 AM 0
Share

still doesn't work ;_; here are the errors it keeps co$$anonymous$$g up with,

Assets/SpecialScripts/EnemyHP.js(15,8): BCE0005: $$anonymous$$ identifier: 'collision'.

Assets/SpecialScripts/EnemyHP.js(16,9): BCE0005: $$anonymous$$ identifier: 'collision'.

Assets/SpecialScripts/EnemyHP.js(16,43): BCE0005: $$anonymous$$ identifier: 'PlayerHealth'. (i know how to fix this)

Assets/SpecialScripts/EnemyHP.js(17,51): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion)' was found.

Assets/SpecialScripts/EnemyHP.js(18,17): BCE0005: $$anonymous$$ identifier: 'collision'.

Assets/SpecialScripts/EnemyHP.js(27,28): BCE0023: No appropriate version of 'UnityEngine.$$anonymous$$athf.Clamp' for the argument list '(int, int)' was found.

Assets/SpecialScripts/EnemyHP.js(31,52): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion)' was found.

Assets/SpecialScripts/EnemyHP.js(32,45): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Vector3, UnityEngine.Quaternion)' was found.

avatar image Kleptomaniac · Mar 11, 2013 at 12:19 AM 0
Share

O$$anonymous$$, that should pretty much work now. Updated post again.

It's made harder by the fact that I don't have Unity on this computer, so I'm having to debug in my brain. :D

avatar image eeveelution8 · Mar 11, 2013 at 12:22 AM 0
Share

everything's fixed :D thanks very much, id uprate you or something if i could but im not allowed to :I

Show more comments

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

Ai that applies damage in collision? 1 Answer

Health Tutorial broken link to script 4 Answers

How can an enemy deplete players health? 0 Answers

AI keeps attacking objects that are not player. 1 Answer

C# Cooldown not working 3 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