Author Topic: So you want to make health bars...  (Read 65019 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: So you want to make health bars...
« Reply #15 on: September 28, 2012, 09:39:30 PM »
For a 3D UI you don't need this. Assuming your health bar is just an object in 3D space, just put a FaceCamera script on it.

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: So you want to make health bars...
« Reply #16 on: October 07, 2012, 03:20:10 PM »
But as the camera that is attached to the UI Root(3D) is not in the same position as the main game camera (that renders the gameobject that I want to add a HUD to), then any 3D UI that it renders won't be in the same position as the gameobject it is tracking. Should I make another NGUI hierarchy that then has a camera that matches the main game camera and renders those types of UI elements?

Or should I attach the NGUI widgets straight to my main game camera so that the 3D UI and the gameobjects are rendered together?

-JJ

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: So you want to make health bars...
« Reply #17 on: October 07, 2012, 07:26:06 PM »
The latter: attach the NGUI widgets straight to your 3D game objects.

terence

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: So you want to make health bars...
« Reply #18 on: October 09, 2012, 01:23:47 AM »
I've been to trying to get a floating health bar on a 3D object to work right. I followed the code in this discussion but the results are less then satisfactory. The UISlider seems to float around the 3D object rather than staying in place.

Info:
1) I am using a simple 2D U. Should I switch to a 3D UI and attach the widgets to my 3D object with a face camera script?
2) I am attaching the uislider to parent to the panel->anchor->camera->uiroot(2d)

Attached some images that might help explain thing. The first is my ui hierachy. The second is the picture of the initial position of the uislider and the third when i have moved the game camera around to the left. It seems to uislider widget seems to float around the object and not stay exact.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: So you want to make health bars...
« Reply #19 on: October 09, 2012, 05:19:49 PM »
Seems to me that it does exactly what it should. You told it to follow an object that has its position slightly below the ground, and your widget's origin is left-aligned.

darthbator

  • Guest
Re: So you want to make health bars...
« Reply #20 on: November 10, 2012, 07:10:26 PM »
I think I posted this on the old forums but I am still having issues getting 2D simple UI elements to track (or even reliably be placed at) 3D scene elements. Here let me show you :) Here's the script I am using to just try and place something in an expected position at start.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class posBar : MonoBehaviour {
  5.         public Transform actorPos;
  6.         public Camera guiCam;
  7.        
  8.         void Start () {
  9.                 guiCam = NGUITools.FindCameraForLayer((int)Layers.Index.screeInterface);
  10.                 Vector3 atmp = Camera.main.WorldToViewportPoint(actorPos.position);
  11.                 atmp.z = 0;
  12.                 Vector3 btmp = guiCam.ViewportToWorldPoint(atmp);
  13.         Debug.Log("ViewOBJ " + atmp.ToString() + "WorldBar " + btmp.ToString());
  14.                 transform.position = btmp;     
  15.         }
  16. }

However my UI elements are ALWAYS being positioned outside of the view of my UI camera. The Z value in particular seems to be tremendously off. What I a most confused about is what is coming out of that debug statement and the actual value of the transform in the editor...

ViewOBJ (3.0, 0.5, 0.0)WorldBar (9.0, 100.0, 0.0)FinalPos (0.5, 100.2, 0.0)

However that doesn't appear to be the value the bar is getting at all.... Check out the attached image....

Why is that happening? I have the UI root placed at worldOrigin + 100. Based on that all the numbers in the debug look correct but I am getting some really out there Z value that is totally throwing my bar out of my UI frame. What am I doing wrong? How am I getting that crazy z value?

« Last Edit: November 10, 2012, 08:49:09 PM by darthbator »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: So you want to make health bars...
« Reply #21 on: November 11, 2012, 10:58:46 AM »
With a 3D camera you don't need this. Just make each health bar object a child of our game object, having its own panel, and a face camera script attached. No UI camera necessary.

darthbator

  • Guest
Re: So you want to make health bars...
« Reply #22 on: November 12, 2012, 05:52:59 PM »
I noticed when I billboard the bars towards the camera with the LookTarget script the bars themselves start to look really jaggy... However when using a dedicated UI camera I am not having this issue (both my game and UI cameras where ortho so that has not changed). Is there any way to mitigate that jagginess without the AA image filter from pro?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: So you want to make health bars...
« Reply #23 on: November 12, 2012, 07:06:34 PM »
Jagginess when it's rotated, you mean? You can just have thicker (and softer) borders.

darthbator

  • Guest
Re: So you want to make health bars...
« Reply #24 on: November 26, 2012, 04:47:28 PM »
What I noticed is that it's not really just jagniess it's that the bars are not "flat" when they are facing the camera making them look really off as they rotate to face the camera and everything moves around the environment. This is why I had initially been using a second camera for the UI. That way I could keep the elements "flat". Maybe there's something I am missing?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: So you want to make health bars...
« Reply #25 on: November 26, 2012, 05:36:29 PM »
3D camera has perspective, so things will look like that. If you want it to always be flat, you need a 2D camera, yes.

AXE

  • Guest
Re: So you want to make health bars...
« Reply #26 on: January 14, 2013, 09:33:26 AM »
I'm using this technique to draw an UILabel with a number over 3D objects in my scene, but I have a problem. Here's the code I'm using:

  1. GameObject selNumberLabel = NGUITools.AddChild(GameObject.Find("SelectionNumbers"), (GameObject)Resources.Load("SelectionNumber"));
  2.     selNumberLabel.GetComponent<UILabel>().text = selIndex.ToString();
  3.    
  4.     Camera gameCam = NGUITools.FindCameraForLayer(realCard.layer);
  5.     Camera UICam = NGUITools.FindCameraForLayer(selNumberLabel.layer);
  6.    
  7.     Vector3 pos = gameCam.WorldToViewportPoint(realCard.transform.position);
  8.         Debug.Log("GameCam pos:" + pos.ToString());
  9.     pos = UICam.ViewportToWorldPoint(pos);
  10.         Debug.Log("UICam pos: " + pos.ToString());
  11.     pos.z = 0;
  12.         Debug.Log("Final pos: " + pos.ToString());
  13.    
  14.     selNumberLabel.transform.position = pos;
  15.         NGUITools.MakePixelPerfect(selNumberLabel.transform);

The objects this label is instantiated into ("SelectionNumbers") is a simple empty GO which is under an Anchor GO (Anchor Center). The position of the empty GO ("SelectionNumbers") is: X = 0, Y = 0, Z = 321. Once the code above gets executed, I'd espect to find the newly instantiated label at X = pos.x, Y = pos.Y, Z = 0. Instead, it always gets a Z = -320.

The debug messages spit out these messages:

1) GameCam pos:(0.7, 0.3, 2.8)
2) UICam pos: (0.7, -0.4, 2.8)
3) Final pos: (0.7, -0.4, 0.0)

The Z should be 0...

Any help?

Scathis

  • Guest
Re: So you want to make health bars...
« Reply #27 on: January 23, 2013, 05:44:22 PM »
Update #2: I found the issue. Took some debug but my main camera was not culling out the UI and therefore returning as the UI camera. Thus the poor offset.

Ok, I'm stumped.

I have:
A 3D Camera for my main camera that can be dragged around.
A UIRoot(2D) with a camera for the UI. UIRoot is located off to the side at (0,0,200)
I have a panel called "PANEL_DYNAMIC" that I parent all my dynamic UI elements to, mostly progress bars. Panel is a child of an object that has an anchor anchoring to the top left. All other position transforms are (0,0,0) and scales are (1,1,1)
I have a UIBarPrefab that is a prefab that has a UIFollowTarget script on it and a child with a label and another child with a progressbar.

This is my code for creating a bar inside the object I want it to follow:
  1. void CreateBarObject()
  2. {
  3.         GameObject pnl = GameObject.Find ("PANEL_DYNAMIC");
  4.         barObject = NGUITools.AddChild( pnl, UIBarPrefab );
  5.         barObject.GetComponent<UIFollowTarget>().target = transform;
  6. }
  7.  

The bar isn't showing up in the right position.
When I look at the update function for UIFollowTarget the barObject position calculates to (10.5,0.0, -1.5) but it's localPosition ends up being (4544.5, -378.8, 0). I'm not sure why. That's way off screen.

Update: No matter how I move around the object on the screen the position/locationposition doesn't change.
I verified the UIFollowTarget script is on the progress bar that gets created but it just doesn't follow the target.

What am I doing wrong? I've combed through as much documentation as I could find.
« Last Edit: January 23, 2013, 07:06:50 PM by Scathis »

Kiu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: So you want to make health bars...
« Reply #28 on: February 21, 2013, 11:43:05 AM »

Thanks for this code, it's working great. I just have one more problem.
I'm using UIlabels for a Augmented Reality App. I'd like to align the label Rotation to the camera z-Rotation so that the labels point to the camera "up" no matter what orientation relative to the screen.

I've tried the euler Angles, but they keep jumping and I'm not really getting how to handle quaternions properly yet.

How would I do this?

hello@patrikstas.com

  • Guest
Re: So you want to make health bars...
« Reply #29 on: May 01, 2013, 03:00:29 PM »
Hey guys, I need your advice how to achieve bar of different shape. I attach pictures so you have clear idea what exactly I need. I mean some more elegant solution than making tons of pictures with different level of progress. Any advice appreciated, thank you.