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 TheDavil · Jun 03, 2011 at 11:28 AM · onguihealthbarworldtoscreenpoint

Health Bar / GUI issues (WorldToScreenPoint)

I'm getting to grips with most of the 3d Stuff in Unity, I have the guts of a tower defence game written, but I'm having trouble with all the GUI stuff.

at the moment I'm just trying to get health-bars to appear above the top of each creep. and I'm not even worried about the shrinking of the healthbar(yet), I just can't get it to appear at the right spot on the screen.

Here's my code:

 var FollowTarget: Transform;
 
 function OnGUI() {
     theCam = Camera.main;
     var screenPos : Vector3 = theCam.WorldToScreenPoint (FollowTarget.position);
     
     HealthBarX = screenPos.x;
     HealthBarY = screenPos.y;
     
     GUI.Box(Rect(HealthBarX,HealthBarY,70,5),"This is a title");
 }

The above code gives the following output:

[http://twitpic.com/56a0kj][1]

(I can't upload to Unity Answers because it just won't take the JPG for some reason)

and I modified some of the code to get it to work on my screen:

 screenHeight = Screen.currentResolution.height;
 HealthBarX = screenPos.x - 35;
 HealthBarY = screenHeight - 170 - screenPos.y;

However that's great for my Resolution in the Unity window, but that will all change depending on resolution so I'm looking for some way to get it to appear directly at the point on screen where the enemy is showing...

Any ideas ? [1]: http://twitpic.com/56a0kj

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
Best Answer

Answer by tnetennba · Jun 03, 2011 at 01:35 PM

This should help you with your issue.

 using UnityEngine;
 using System.Collections;
 public class NewBehaviourScript : MonoBehaviour {

 public Transform target;
 private static int healthbarwidth = 70;


 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void OnGUI()
 {
     Camera cam = Camera.main;

     Vector3 screenPos = cam.WorldToScreenPoint(target.position);

     Debug.Log(screenPos.ToString());

     GUI.Box(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y, 70, 5), "Title");

 }

}

The problem you were facing is that the Camera and screen use the y coordinate in an opposite manner to each other. So your Y coordinate is actually:

 Screen.height - screenPos.y 
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 TheDavil · Jun 03, 2011 at 02:17 PM 0
Share

ok so I used a JS version of your code above ,but the bar appeared far below the item on screen, so I added some Vector3.forward to it - as in the following line var screenPos : Vector3 = theCam.WorldToScreenPoint (FollowTarget.position + Vector3.forward*62 );

and then that seems to work in Unity Editor but when I compile the GUI box doesn't appear ? any ideas ?

avatar image tnetennba · Jun 03, 2011 at 02:27 PM 0
Share

I did a quick javascript version:

var target: Transform;

var healthbarwidth : int = 70;

function Update () { }

function OnGUI(){ var screenPos : Vector3 = Camera.main.WorldToScreenPoint(target.position); Debug.Log(screenPos.ToString()); GUI.Box(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y, 70, 5), "Title"); }

This works for me.

avatar image TheDavil · Jun 03, 2011 at 02:54 PM 1
Share

yep you're right. I must have missed something in my code but your last JS version there works great... even after compile - Thanks so much !!!

avatar image tnetennba · Jun 03, 2011 at 03:01 PM 0
Share

no problem

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to have a GUI button be made at the position of it's attached transform? 1 Answer

OnGUI() not showing anything 2 Answers

Getting the 2d coordinates of an object 2 Answers

RTS health bars 2 Answers

Plzz help me here.... 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