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 Apriyanto · Apr 09, 2013 at 12:23 AM · enemyhealthbarenemydamage

[help] enemy Health Bar decrease at the same time

Hi all~ i have problems on my enemy health bar, when i try to hit single enemy the health bar on all enemies will decrease at the same time, i don't know what happen in here. Can someone help me Fix this ?

alt text

here is my scripts Player attack :

     var targets : GameObject [];
     var attackTimer : float;
     var coolDown : float;
     var prefabBullet : Transform;
     var shootForce : float;
     var hand : Transform;
     var fireRate : float = 1.4f;
     
     public var playerTarget : Transform;
     public var HealthEnabled:boolean = false;
  
     public var lastPlayerTarget;
     
     private var nextFire : float = 0f;
     
     
     // Use this for initialization
     function Start () {
     
         targets = GameObject.FindGameObjectsWithTag("Enemy");
         attackTimer = 0;
         coolDown = 1.0f;
     
     }
     
     // Update is called once per frame
     function Update () 
     {
     
         
           if(Health == 0)
         {
         
         PlayerAnimation.Stop("dead");
         }
 
         if(attackTimer > 0)
             attackTimer -= Time.deltaTime;
         if(attackTimer < 0)
             attackTimer = 0;
         if(Input.GetKeyUp(KeyCode.Mouse0) || Input.GetKey (KeyCode.Return) && prefabBullet.rigidbody)
         {
             
             if(attackTimer == 0)
             {
                 if( Health.health > 0 && EnemyHealthJava2.healthBarLength > 0)
                 {
                 //Health.health > 5;
                 Attack();
                 attackTimer = coolDown;
                 }
                 
             }
         }
     
     }
     
     function Attack()
     { 
         
            for (var i = 0; i < targets.Length; i++)
         {
         
         
             
          var distance : float = Vector3.Distance(targets[i].transform.position, transform.position);
  
          var dir : Vector3 = (targets[i].transform.position - transform.position).normalized;
  
          var direction : float = Vector3.Dot(dir, transform.forward);
  
         
         if(distance < 10)
         {
             if(direction > 0)
             {
                 
                 nextFire = Time.time + fireRate;
                 var clone : Transform = Instantiate(prefabBullet, hand.position, hand.rotation) as Transform;
                 clone.rigidbody.velocity = transform.TransformDirection(Vector3.forward * 20);
                 
                 animation.Play("shoot");
                 var eh : EnemyHealthJava2 = targets[i].GetComponent("EnemyHealthJava2");
                 eh.AdjustCurrentHealth(-10);
             
             }
         }
     
         }    
     }
 

here is the enemy health:

 var playerName : String;
 var adjustment : float= 2.3f;
 var healthTex : Texture2D;
 
 
 
 static var health : int = 100;
 static var curHealth : int = 100;
 static var enemy : String;
 static var alive : boolean = true;
 static var healthBarLength : float = 100;
 
 
 private var worldPosition : Vector3= new Vector3();
 private var screenPosition : Vector3= new Vector3();
 private var myTransform : Transform;
 private var myCamera : Camera;
 private var healthBarHeight : int= 5;
 private var healthBarLeft : int= 110;
 private var barTop : int= 1;
 //private var healthBarLength : float= 100;
 private var labelTop : int= 18;
 private var labelWidth : int= 110;
 private var labelHeight : int= 15;
 
 private var myStyle : GUIStyle= new GUIStyle();
 
 
 
 function Awake ()
 {
     myTransform = transform;
     myCamera = Camera.main;
     myStyle.normal.textColor = Color.green;
     myStyle.fontSize = 12;
     myStyle.fontStyle = FontStyle.Bold;
     myStyle.clipping = TextClipping.Overflow;
 }
 function Update () {
 
     if (healthBarLength != null && curHealth <= 0)
         {
             
             
             Destroy(gameObject);
             Debug.Log("Enemy Death");
             return;
         }
 
 }
 
 function OnGUI () 
 {
 
     worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,myTransform.position.z);
     screenPosition = myCamera.WorldToScreenPoint(worldPosition);
 
     GUI.Box(new Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop,100, healthBarHeight), "");
     GUI.DrawTexture(new Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop,healthBarLength, healthBarHeight), healthTex);
     GUI.Label(new Rect(screenPosition.x - labelWidth / 2, Screen.height - screenPosition.y - labelTop, labelWidth, labelHeight), playerName, myStyle);
 }    
 
 
 static function  UpOrDown (amount : int)
 {
 
     healthBarLength += amount;
 }
 
 
 public function AdjustCurrentHealth( adj : int) 
 {
 
     healthBarLength += adj;
 
     if(healthBarLength <=0 && alive)
     {
         alive = false;
         healthBarLength = 0;
     
 }
     if(healthBarLength > health)
     healthBarLength = health;
 
     if(health < 1)
     health = 1;
 }
     
 
Comment
Add comment · Show 1
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 Apriyanto · Apr 08, 2013 at 10:49 PM 0
Share

Sorry little bit fixed by my self in here Unity Forum

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LyanApps · Apr 09, 2013 at 02:13 AM

You don't want to use the keyword static in static var health : int = 100; That means one instance of this variable for all instances of your class.

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

Answer by Apriyanto · Apr 09, 2013 at 06:52 AM

SOLVED problems , can see in here http://forum.unity3d.com/threads/177470-help-enemy-Health-Bar-decrease-at-the-same-time?p=1214131&posted=1#post1214131

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

My healtbar is not change when i look another enemy 0 Answers

C# Floating Health Bar 2 Answers

Best way to make a health par over enemies 2 Answers

How can I do health bar follows enemy? 1 Answer

Enemy HealthBar Problems 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