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 Ekta-Mehta-D · Oct 02, 2013 at 07:42 AM · guienemyrecthealth

Rect Following Object : Unity3d

Hello everyone ,

I want to position health bar[Rect] above the enemy. I have written script for health bar[Rect] and it works as per my requirement. But as per my code, it is not coming on the top of enemy object. My enemy object is "Plane game object".

Code which i have written is as below :

 #pragma strict
 public var barIdle : Texture2D;
 public var barFull : Texture2D;
 public var initialWidth : float;
 public var barRect : Rect;
 public var maxHealth : float;
 public var curHealth : float; 
 var offset = Vector2(0, 1.5);
 
 
     
 function Start () {
 }
 
     function Update () {
     var point = Camera.main.WorldToScreenPoint(transform.position + offset);
     barRect = new Rect( point.x , point.y, 70, 15);
     initialWidth = barRect.width;
 }
 
 function OnGUI()
 {
     GUI.DrawTexture(barRect, barIdle);
     GUI.BeginGroup(new Rect(barRect.x, barRect.y, ((initialWidth * curHealth) / maxHealth), barRect.height));
     GUI.DrawTexture(new Rect(0, 0, barRect.width, 15), barFull);
     GUI.EndGroup();
     Debug.Log("barRect x: " + barRect.x + " barRect y: " + barRect.y);
 }

and i am attaching this script to my enemy object as i want to manage health for each enemy.

Please help me and solve my problem..

Thanks in advance for your support and help..

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 mattssonon · Oct 02, 2013 at 10:02 AM 0
Share

I assume your game is in 2D, where does the bar actually appear?

avatar image VioKyma · Oct 02, 2013 at 10:23 AM 0
Share

You have a fixed value for the adjustment of your bar's y position. I would suggest adjusting the y position relative to the height of your enemy

avatar image Ekta-Mehta-D · Oct 02, 2013 at 10:49 AM 0
Share

hmm i need to adjust y position of rect.. i have done Screen.height - point.y. But that does tot appear on the top..

avatar image VioKyma · Oct 02, 2013 at 11:25 AM 0
Share

Is your enemy a 3D object, or a 2D sprite? Do you mean that it appears behind your enemy, or below?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by raimon.massanet · Oct 02, 2013 at 01:09 PM

From my understanding, the problem is that Camera.WorldToScreenPoint returns a point, based on the bottom-left corner of the screen. That is, the bottom-left corner of the screen is (0,0) and a higher y means the object is further up. On the other hand, the top parameter of the constructor for Rect means how far away from the screen top the rectangle is, that is, the top-left corner of the screen is (0,0) and a higher top means the rectangle is further down.

So, your Rect should be placed at:

barRect = new Rect(point.x, Screen.height - point.y, 70, 15);

If you solve that, your bar shold be positioned as expected.

Comment
Add comment · Show 1 · 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 Iman-x · May 15, 2016 at 05:09 PM 0
Share

Thanks for perfect answer

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

18 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

Related Questions

Taking a hit 3 Answers

Make HUD to show object direction 0 Answers

Deduct health on collision 2 Answers

Input.mouse position on GUI Textures 2 Answers

Enemy Health GUI Issues 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