• 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 FizzyBear · Apr 16, 2012 at 03:23 AM · animationcolliderattacking

Health bar isn't decreasing.

This doesn't seem to decrease on every collide. Yes I've set the sword to have a box collider. But nothing seems to work.

 @script ExecuteInEditMode()
 var healthTexture : Texture2D;
 var healthBorder : Texture2D;
 private static var health : int = 100;
 
 function OnGUI () {
     
     GUI.DrawTexture(Rect(0,Screen.height - 600,400,100), healthBorder);
 
     //Now on top we can put Health Bar texture
     var adjust : int = health * 3;                       //adjusting texture size (width) / health(100)
     GUI.BeginGroup(Rect(135,Screen.height - 565,adjust,15));
     GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
     GUI.EndGroup();
 }
 
 /**
     Function to handle collision
 */
 function OnTriggerEnter(other:Collider)
 {
       health -= 10;
 }
Comment
Add comment · Show 4
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 kolban · Apr 16, 2012 at 03:37 AM 0
Share

Put a debug statement in your onTriggerEnter and validate that the trigger is being called.

avatar image IgnoranceIsBliss · Apr 16, 2012 at 03:39 AM 0
Share

Wouldn't it be easier to drop the 'Group' and change the size of the texture rect?

Note - you may have to add code to deal with different aspect ratios if you want the health texture image to not stretch on other window/layout systems.

avatar image FizzyBear · Apr 16, 2012 at 03:46 AM 0
Share

@kolban it is being validated.

@IgnoranceIsBliss - At this stage it isn't much of a worry for me as I'd prefer to get the game functionality working before I focus on the design.

avatar image IgnoranceIsBliss · Apr 16, 2012 at 03:58 AM 0
Share

Ahh, are you using BeginGroup and EndGroup to as a way to clip your texture so it doesn't visibly contract as the health bar gets shorter.

In your OnTriggerEnter, after you subtract from the health amount, use Debug.Log to actually let you know the health amount. Same for your OnGUI function.

You may find it creates a LOT of messages in your console, but it will show for a fact that it is an issue with your data rather than an issue with your drawing code.

2 Replies

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

Answer by FizzyBear · Apr 19, 2012 at 04:46 AM

Fixed this by calling the component of the health variable.

 function OnTriggerEnter (d:Collider)
 {    
     print ("DIE");
     if (animation.Play("attack")== true) {
         GetComponent(Motor).health -= Random.Range(0,10);
     }
 }
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 darkcookie · Apr 16, 2012 at 04:57 AM

if i was you i would do it like this .......

c#...

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
     public int maxHealth = 100;
     public int curHealth = 100;
     public GameObject Drop;
     public GameObject RagDroll;
     public float healthBarLength;
     public GUIStyle myStyle;
     
     // Update is called once per frame
     void Start ()
     {
         healthBarLength = Screen.width / 3;
     }
     
     void Update () 
     {
        AddjustCurrentHealth(0);
     }
     
     void OnGUI ()
     {
         GUI.Box(new Rect(10, 32, healthBarLength, 20), curHealth + "/" + maxHealth, myStyle);
     }
     
     public void AddjustCurrentHealth(int adj)
     {
         curHealth += adj;
         
         healthBarLength = (Screen.width / 3) * (curHealth / (float)maxHealth);
         
         if(curHealth < 0)
         {
             Die ();
         }
         if(curHealth > maxHealth)
             curHealth = maxHealth;
         if(maxHealth < 1)
             maxHealth = 1;
     }
     
     
     public void Die ()
     {
         Destroy(gameObject);
         Instantiate(Drop, transform.position, transform.rotation);
         Instantiate(RagDroll, transform.position, transform.rotation);
     }
 }
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 darkcookie · Apr 16, 2012 at 05:07 AM 0
Share

now if u want is to take damage from an enemy ....i would do something like this.....

using UnityEngine; using System.Collections;

public class EnemyAttack : $$anonymous$$onoBehaviour { public GameObject target; public int damage = 10; public float attackTimer; public float coolDown;

 // Use this for initialization
 void Start ()
 {
     attackTimer = 0;
     coolDown = 7.0f;
 }
 
 // Update is called once per frame
 void Update () 
 {
    if(attackTimer > 0)
         attackTimer -= Time.deltaTime;
     
    if(attackTimer < 0)
         attackTimer = 0;
         
    
 
   if(attackTimer == 0)
         {
            Attack();
            attackTimer = coolDown;
         }
     
 }
 
 // attack
 void Attack () 
 {
     float distance = Vector3.Distance(target.transform.position, transform.position);
     Vector3 dir = (target.transform.position - transform.position).normalized;
     float direction = Vector3.Dot(dir, transform.forward);
     
     if(distance < 6f)
     {
         if(direction > 0)
         {
             PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
             ph.AddjustCurrentHealth(-damage);
             animation.CrossFade("attack",0.0f);
             animation.Stop("idle");
             animation.Stop("run");
         }
     }
 }
     

}

avatar image FizzyBear · Apr 16, 2012 at 06:15 AM 0
Share

Getting null reference on this line:

 ph.AddjustCurrentHealth(-damage);

How do I fix that?

avatar image IgnoranceIsBliss · Apr 16, 2012 at 06:22 AM 0
Share

That would mean you don't have a 'PlayerHealth' component on the object that is taking damage (your player).

$$anonymous$$ake sure you've selected your player object and chosen 'PlayerHealth' from the Components|Scripts menu.

In order to CHANGE your player health, the script has to first get access to it. The PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth"); line does this by asking for the 'PlayerHealth' script from the target object.

avatar image FizzyBear · Apr 16, 2012 at 07:23 AM 0
Share

Ah yeah, I just fixed that. But now I'm getting

It's still not removing the 10 HP from the health :/

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

collider animation 1 Answer

Unity 4.5.2 bug changing the properties of the 2d colliders in animation 1 Answer

Triggering door animation with collider 2 Answers

Play animation when colliding with trigger! 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