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 Jojjy · Jul 12, 2012 at 03:01 AM · cameraguitextureduplicatelifebar

Problem with lifebar guiTexture duplicated

Hi to all. I'm working on a life bar for the enemies. Each enemy is correctly instantiate with his own lifebar. In the scene I have a first person controller.

Now, the problem is that if I turn 180 degrees and look behind, the life bars are duplicated almost like in a mirror. It's not an editor issue, persists in the build too. It's a camera problem I guess but I don't know how to fix it.

You can see here what happen.

alt text

The object myHealthBar is a guiTexture. The code is:

 #pragma strict
 // Health bar variables
 var hp : float = 100;
 var maxHp : float = 100;
 var healthBarWidth : int;
 var healthBarHeight : int;
 var healthBarStretch : int;
 var myHealthBar : GameObject;
 var myHb : GameObject;
 // Colors
 var colorStart : Color;
 var colorEnd : Color;
 
 
 function Start ()
 {
  myHb = Instantiate (myHealthBar, transform.position, transform.rotation);
 }
 
 function Update ()
 {
  // Health bar
  myHb.transform.position = camera.main.WorldToViewportPoint(transform.position);
  myHb.transform.localScale = Vector3.zero;
  var healthPercent : float = hp / maxHp;
  if (healthPercent < 0){healthPercent = 0;}
  if (healthPercent > 100){healthPercent = 100;}
  healthBarStretch = healthPercent * healthBarWidth;
  myHb.guiTexture.color = Color.Lerp (colorEnd, colorStart, healthPercent);
  myHb.guiTexture.pixelInset = Rect(-healthBarWidth/2, 20, healthBarStretch, healthBarHeight);
  damage();
 }
 
 function damage ()
 {
  hp = hp - .1;
  if (hp < 0){hp = 0;}
 }




Excuse me for my bad english, I'm italian. Thanks in advance.

unity_answer_002.jpg (87.7 kB)
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
0

Answer by whydoidoit · Jul 12, 2012 at 06:41 AM

Your problem is that WorldToViewportPoint will map the world point onto the viewport even if it is behind you (this is by design and is occasionally very useful = but often causes this problem). Don't draw your health bar when the enemy is behind you.

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 whydoidoit · Jul 12, 2012 at 06:48 AM 0
Share

By the way you can test whether it is behind you by doing this:

    if(Vector3.Dot(transform.forward, target.position - transform.position)>=0) {
        //It's in front of you
    }
avatar image Jojjy · Jul 17, 2012 at 07:34 PM 0
Share

Thanks for the hints. I solved the problem adding these lines.

 if (inSight)
  {
  myHb.guiTexture.pixelInset = Rect(-healthBarWidth/2, 20, healthBarStretch, healthBarHeight);
  }
  else
  {
  myHb.guiTexture.pixelInset = Rect(0, 0, 0, 0);
  }
 }
 
 function OnBecameVisible()
 {
     inSight = true;
 }
 
 function OnBecameInvisible()
 {
     inSight = false;
 }


I think that this is the simplest way.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiplying GUIs 0 Answers

Triggering Main Camera Script with GUITexture 1 Answer

GuiTexture and Split Screen 1 Answer

Camera problems 2 Answers

GUI Texture - from Box to Camera 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