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 MythicEclipsed · Aug 01, 2016 at 03:01 AM · scripting problem

How to keep my health bar directly above my player and straight no matter how the player turns?

Health Bar issue: https://i.gyazo.com/cfa5ee306193bdea7649821ea1b63933.gif How can I keep it straight horizontally above my player at all times no matter which direction my player turns? Health Bar Script:

  using UnityEngine;
  using UnityEngine.UI;
 
 public class StatusIndicator : MonoBehaviour {
 
     [SerializeField]
     private RectTransform healthBarRect;
     [SerializeField]
     //private Text healthText;
 
     void Start()
     {
         if (healthBarRect == null) 
         {    
             Debug.LogError ("StatusIndicator Bar is Missing");
         }
 
         //if (healthText == null) 
         //{    
             //Debug.LogError("StatusIndicator Bar is Missing");
         //}
     }
 
     public void SetHealth(int _cur, int _max)
     {
         float _value = (float)_cur / _max;
 
         healthBarRect.localScale = new Vector3(_value, healthBarRect.localScale.y, healthBarRect.localScale.z);
         //healthText.text = _cur + "/" + _max + "HP";
     }
 
 }
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
4

Answer by superbsumit · Aug 01, 2016 at 11:28 AM

One way to achieve this is to make an empty gameObject as your player, and make the player sprite and health bar as its children. Rotate the player sprite with the script attached on that empty gameObject or as you wish. This way you can achieve the same effect.

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 MrReynevan2 · Aug 01, 2016 at 11:05 AM

You can unparent it from Player and add something like

 pp = player.transform.position;
 healthBar.transform.position.Set(pp.x, pp.y+0.5f, pp.z);

to your Update() function.

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 MythicEclipsed · Aug 01, 2016 at 08:47 PM 0
Share

How should I assign pp. Also its saying x,y,z don't exist in the current state.

avatar image MrReynevan2 MythicEclipsed · Aug 02, 2016 at 12:07 PM 0
Share

create a public variable player of type GameObject, and in Start() method assign to it the player:

 player = GameObject.Find("Player");

Then create another public field called pp of type Vector3 and update it in Update() function.

Also, I might have made a mistake with the Vector3.Set() function. It doesn't seem to work. Ins$$anonymous$$d try:

 healthBar.position = new Vector3(pp.x, pp.y+0.5f, pp.z);

Declare healthBar the same way as the player. Do you follow? If you have problems with that I can write the entire code for you later today.

avatar image
1

Answer by mrpmorris · Aug 01, 2016 at 11:14 AM

You could draw it in the OnGUI() method of a MonoBehavior

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

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

Have an object only visible when input is held down 1 Answer

Need help with dialogue system script 0 Answers

Is there a reason why Unity would be able to read one variable in a script and not another? 1 Answer

After exporting my project from my PC to my laptop the physics of my game work differently. 1 Answer

Left side panel in monodevelop 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