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 /
  • Help Room /
avatar image
0
Question by RobbyT15 · Oct 18, 2016 at 10:19 PM · c#unity5

Anchor a health bar to a character/enemy

I have a game I'm working on where the player can see the health bars of the player's avatar and other creatures in the game. I want this health bar to always follow the character/creature around. However, I can't figure out how to do this. I thought having the position change when the respective object moved would do the trick, but apparently, it doesn't. When watching the health bar's transform, however, it shows that it is moving with it's respective object but I don't see it on the game screen. Can anyone explain how I go about accomplishing this? Here's the Dropbox link to the project.

Dropbox Link YouTube video of what it's currently doing

HealthBar Script

 public class EnemyHealthBar:MonoBehaviour{
     public Texture2D healthBar;
     public Texture2D health;
     public Rect healthBarPosition;
     public Rect healthPosition;
     public GameObject monster;
 
     void Start(){
     }
     void Update(){
         transform.position = new Vector3(monster.transform.position.x, monster.transform.position.y, monster.transform.position.z);
         Debug.Log(transform.position);
     }
     void OnGUI(){
         GUI.DrawTexture(healthBarPosition, healthBar);
     }
 }


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
1

Answer by Qvothe · Oct 19, 2016 at 02:31 AM

  1. Make sure that your character is the parent to the healthbar, if the character is the parent object, then the healthbar will move (without any coding) so that its position remains relative to its parent.

  2. You're going to need to add a "billboard" property to the healthbar. The "billboard" property makes sure that the healthbar is always perpendicular to the screen, so that no matter what your characters orientation in the game world, the text/image you're trying to display always shows correctly.

Also, you might need to adjust the position of the healthbar relative to the screen space. I haven't messed with this in a while, but iirc you have to adjust the Ypos by diving by 2 or something similar.

Honestly, it would probably be better/easier to make a generic prefab out of your healthbar, then instantiate it during runtime and make your character's avatar the parent object. This way you can edit the healthbar in play mode to get it exactly where you want it and how you want it to look instead of generating it via code at runtime only.

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
1

Answer by OutOfRam · Oct 18, 2016 at 10:54 PM

Have you tried simply parenting the healthbar to the monster instead of manually trying to move it? An object always stays in the same position relative to it's parent, unless otherwise told, so makeing the monster be the parent of the healthbar will keep it in the monsters local space

you can parent the object in the editor by dragging the healthbar object onto the monster object; or if it is instantiated mid game, you can do it through code using

 healthBar.transform.parent = monster.transform;


I hope this helps!

Comment
Add comment · Show 2 · 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 RobbyT15 · Oct 19, 2016 at 12:13 AM 0
Share

I had it set as a child in the inspector thinking that would do it, but I always got the same results which made no sense to me as it should work, but it just stays in the top left hand corner of the screen and not over the parent object.

avatar image OutOfRam RobbyT15 · Oct 19, 2016 at 12:46 AM 0
Share

Well, is the healthbar part of a UI interface(an actual UI component), or is it a physical object, because the two should be handled differently?

avatar image
1

Answer by OusedGames · Oct 19, 2016 at 12:38 AM

On my games I use a Canvas on top of the enemy:

  • and set the render mode of the canvas to "World Space"

  • inside the canvas I use an UI slider

  • And put this code on the canvas

    public class BarFollow : MonoBehaviour {

       public Transform enemyPos;
         Vector3 offset;
         
        void Start () {
             offset = transform.position - targetPos.position;
         }
         void Awake() {
             offset = transform.position - targetPos.position;
         }
     
         void LateUpdate () {
             transform.position = targetPos.position + offset; 
             transform.LookAt(Camera.main.transform.position);
         }
     }
    
    

Hope it works!!

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

8 People are following this question.

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

Related Questions

Move a separate object forward on collision between particle system and another object 0 Answers

Is there possibility to have 1 Script for Different SceneSwitches? 2 Answers

I guys I have a really hard question 0 Answers

Is there a way to make a script activate when you look at an object? 1 Answer

Increase Score When Moving Up (Script Help) 0 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