Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 matthew_tavares1 · Oct 03, 2011 at 08:34 PM · guihealth

GUI appears even when placed behind an object.

I've programmed my enemy's health exactly how I wanted it, the size place, and how it works. However if I place an object in front of it like a wall the enemy's health still appears on screen even though the enemy isn't. Can someone please help me make it so that when the enemy is behind an object it doesn't show its health bar? I've already tried viewport instead of world, and that didn't work.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : MonoBehaviour
 {
     public int maxHealth = 100; 
     public int currentHealth = 100;
     
     public float healthBarLength;
     
     public Transform target;
     private int healthbarwidth = 70;
     //public bool isVisible;
 
     void Start()
     {
         healthBarLength = Screen.width / 2;
     }
     
     // Update is called once per frame
     void Update()
     {
         AddjustCurrentHealth(0);
         
         if (currentHealth == 0) {
             Destroy(gameObject); 
         }
     }
     void OnGUI()
     {
         
         Camera cam = Camera.main;
         
         Vector2 screenPos = cam.WorldToScreenPoint(target.position);
         
     GUI.Box(new Rect(screenPos.x - healthbarwidth / 2, Screen.height - screenPos.y - 30, 70, 20), currentHealth + "/" + maxHealth);
   
     }
     public void AddjustCurrentHealth(int adj){ 
         currentHealth += adj;
 
         if (currentHealth < 1)
             currentHealth = 0;
 
         if (currentHealth > maxHealth)
             currentHealth = maxHealth;
 
         if (maxHealth < 1)
             maxHealth = 1;
 
         healthBarLength = (Screen.width / 2) * (currentHealth / (float)maxHealth);
     }
 }
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
1

Answer by aldonaletto · Oct 03, 2011 at 08:45 PM

All GUI elements appear in front of everything else in the 3D world - it's like a glass window in front of the camera, where all GUI stuff is rendered, so there's no chance for some 3D world object to occlude a GUI item. The only alternative is to use a 3D object (a cube, for instance) as your health bar.

EDITED: You can detect when health has changed, and show the health bar during some time. Declare these variables before OnGUI, and modify the OnGUI function as follows:

public float duration = 2f; // time to display health bar private float endTime = 0f; private int lastHealth = 100;

void OnGUI(){ if (currentHealth != lastHealth){ // if health changed... endTime = Time.time + duration; // set timer to show message lastHealth = currentHealth; // and update lastHealth } if (Time.time > endTime) return; // does nothing if time elapsed // the code below this point has not changed Camera cam = Camera.main; Vector2 screenPos = cam.WorldToScreenPoint(target.position); GUI.Box(new Rect(screenPos.x - healthbarwidth / 2, Screen.height - screenPos.y - 30, 70, 20), currentHealth + "/" + maxHealth); }

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 matthew_tavares1 · Oct 03, 2011 at 10:23 PM 0
Share

I didn't know that, I'm still new to unity so I still have a lot to learn. Would it be possible to make it appear once the player hits the enemy ins$$anonymous$$d of just always being there? If so, how would I go about doing it?

avatar image vxssmatty · Oct 03, 2011 at 10:51 PM 0
Share

Yes you can :) ... you could make a timer that flags a boolean for x amount of time and then disables it again on triggering a damage function or a collision.

avatar image aldonaletto · Oct 04, 2011 at 02:29 AM 0
Share

Like @vxssmatty said, you can use most of your code with some changes to show the health bar only when the health is modified. I edited my answer to include these changes.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Health Bar 1 Answer

Health Bar Centering 4 Answers

Health and ammo GUI not working on re-spawn 0 Answers

ScrollWindow AutoUpdating value help :( 1 Answer

Issue with GUITexture width. 1 Answer


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