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 moko · Jul 20, 2013 at 01:02 PM · guitransformpositionhealthbarworldtoscreen

HealthBar for 5 Objects

Hello,

I am using the following script to display a healthbar for each object in my scene:

var soldier: Transform; var healthbarwidth : int = 80;

function OnGUI() { var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position); GUI.backgroundColor = Color.green; GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 80, 20),"Health"); }

My Problem now: As you can see I am referring to the Camera.main.WorldToScreenPoint of my object. It works fine but Unity displays each healthbar twice in the scene. In front of the camera and if I rotate the camera by 180 degrees there are the same healthbars again.

What's the problem of my script?

Thanks for your help :)

Comment
Add comment · Show 2
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 rednax20 · Jul 20, 2013 at 05:01 PM 1
Share

please format your code.

avatar image moko · Jul 20, 2013 at 05:08 PM 0
Share

sry for that:

var soldier: Transform;

var healthbarwidth : int = 80;

function OnGUI() {

var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position);

GUI.backgroundColor = Color.green;

GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 80, 20),"Health");

}

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Adamcbrz · Jul 21, 2013 at 10:20 PM

I don't think there is anything wrong with your script. The problem is that in both cases there wordltoscreenpoint return the same location. These ease solution is to check if the direction towards your object from the cameras forward is less then 90.

 soldiersDirection = (soldier.transform.position - camera.transform.position).normalized;
 float angle = Vector3.Angle(camera.transform.forward, soldiersDirection);
 if(angle > 90)
 {
      // hide healthbar
 }

That should help. Realize this is sent from my phone so there might be some typing errors.

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
0

Answer by moko · Jul 21, 2013 at 10:14 PM

I found a hint: http://answers.unity3d.com/questions/483118/gui-behind-camera-worldtoscreenpoint.html

...and tried to improve my code:

var soldier: Transform;

var healthbarwidth : int = 50;

var heading = soldier.position - Camera.main.transform.position;

function OnGUI() {

var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position);

if (Vector3.Dot(camera.transform.forward, heading > 0){

GUI.backgroundColor = Color.green;

GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 50, 20),"Health");

}

}

BUT it's not working :(

any idea?

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
0

Answer by IgorAherne · Jul 21, 2013 at 10:21 PM

 var soldier: Transform;
 
 var healthbarwidth : int = 80;
 
 function OnGUI() {
 
 var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position);
 var normScreenPos : Vector3 = soldier.position - transform.position;
 normScreenPos = normScreenPos.normalized;
 GUI.backgroundColor = Color.green;
 
 if(Vector3.Dot(normScreenPos, transform.forward) > 0) //change value of greater to less than if needed
 GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 80, 20),"Health");
 
 }
 
 
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 moko · Jul 22, 2013 at 09:54 AM 0
Share

thanks guys. that helped a lot! :)

for anyone who's interested. thats my code now:

 var soldier : GameObject[];

 var healthbarwidth : int = 50;

 var GUIEnabled = false;

 function Start () {

 soldier = GameObject.FindGameObjectsWithTag("soldier");

 }

 function OnGUI() {

 for(var t : int = 0 ; t < soldier.length ; t++){

 var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier[t].transform.position);

 var normScreenPos : Vector3 = soldier[t].transform.position - transform.position;

 var soldiersDirection = (soldier[t].transform.position - camera.transform.position).normalized;

 var angle : float = Vector3.Angle(camera.transform.forward, soldiersDirection);

 if(GUIEnabled) { 

 GUI.backgroundColor = Color.green;

 GUI.Button(new Rect(screenPos.x - healthbarwidth/2f, Screen.height - screenPos.y - 115f, 50f, 20f),"Health");

 }

 if(angle < 90){

 GUIEnabled = !GUIEnabled;

 }

 }

 }

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

17 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

Related Questions

How to set the position of a guitext using transform? 1 Answer

Trying to set up some GUI using planes and textures. Need help with dynamic changes. 0 Answers

positioning gui! 2 Answers

lock GUITexture on the screen at specific co - ordinates 3 Answers

GUI item with object position+dimensions 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