Author Topic: Show UILabel/Sprite above Player?  (Read 5567 times)

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Show UILabel/Sprite above Player?
« on: April 17, 2014, 05:02:33 PM »
We're working on a 2D game, and sometimes our Character talks with others while the Main-Camera is centered.
We want them to have Speech-Bubbles with Text above their heads.
We thought it would be nice to achieve that with NGUI.

Is there a quick way to display an UILabel/Sprite from UIRoot and it's camera over the position of the 2D-Player-Sprite and his MainCamera?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #1 on: April 18, 2014, 03:56:05 PM »
Look at anchoring. UIRect's documentation page.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #2 on: April 19, 2014, 08:32:17 AM »
Alright. I had a look and it is indeed what I was searching for. The problem is now: I can't get the desired results. Have a look:

So this is my Label in the center of the UI (The label is a child of a Panel)


As you see the Target is the Player-Transform.

But as soon as I hit play, the Label moves to the lower right of the UI.


Of course in the Game-View the Label is displayed in the lower right. I want it to "hover" above the Player.

Wrong settings?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #3 on: April 19, 2014, 11:12:44 PM »
Likely layer confusion. Check the layers the player object is on, and which layers which cameras can see. Make sure that each layer can only be seen by one camera.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #4 on: April 20, 2014, 01:12:51 AM »
The Player is on "Default" and the MainCamera renders Default and some other Layers but not the GUILayer.
My UI-Camera renders only the "GUILayer".

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #5 on: April 20, 2014, 04:15:23 PM »
Make sure the game view and the scene view are both visible. If the game view is hidden behind the scene view, it's not possible to determine its dimensions due to how Unity works.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #6 on: April 20, 2014, 04:37:18 PM »
Both are visible at the same time!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #7 on: April 20, 2014, 05:22:21 PM »
Then I don't know what's wrong in your case.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #8 on: April 21, 2014, 09:30:13 AM »
Mhm, I figured something out. I have a script which is responsible for a smooth (Main)Camera-Follow.
I noticed, when I remove that script, it works how it should!

This is what's happening in the script:

  1. void Update ()
  2.         {
  3.                 Vector3 _tmp = thisTransform.position;
  4.  
  5.  
  6.  
  7.                 if(target.position.y < level_minY)
  8.                 {
  9.                         _tmp.y = Mathf.SmoothDamp(thisTransform.position.y, level_minY,
  10.                                                   ref velocity.y, smoothTime);
  11.                         thisTransform.position = _tmp;
  12.                 }
  13.                 else if(target.position.y > level_minY && target.position.y < level_maxY)
  14.                 {
  15.                         _tmp.y = Mathf.SmoothDamp(thisTransform.position.y, target.position.y,
  16.                                                   ref velocity.y, smoothTime);
  17.                         thisTransform.position = _tmp;
  18.                 }
  19.                 else if(target.position.y > level_maxY)
  20.                 {
  21.                         _tmp.y = Mathf.SmoothDamp(thisTransform.position.y, target.position.y,
  22.                                                   ref velocity.y, smoothTime);
  23.                         thisTransform.position = _tmp;
  24.                 }
  25.  
  26.                 if(target.position.x <= level_minX)
  27.                 {
  28.                         _tmp.x = Mathf.SmoothDamp( thisTransform.position.x,
  29.                                                   level_minX, ref velocity.x, smoothTime);     
  30.                         thisTransform.position = _tmp;
  31.                 }
  32.                
  33.                 // ABSOLUTER PUNKT RECHTS
  34.                 else if(target.position.x >= level_maxX)
  35.                 {
  36.                         _tmp.x = Mathf.SmoothDamp( thisTransform.position.x,
  37.                                                   level_maxX, ref velocity.x, smoothTime);
  38.                         thisTransform.position = _tmp;
  39.                 }
  40.                
  41.                 // ANSONSTEN FOLGEN
  42.                 else
  43.                 {
  44.                         _tmp.x = Mathf.SmoothDamp( thisTransform.position.x,
  45.                                                   target.position.x, ref velocity.x, smoothTime);
  46.                         thisTransform.position = _tmp;
  47.                 }
  48.         }


What sould I change in order to get it to work with the Camera-Follow?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #9 on: April 22, 2014, 04:17:56 AM »
Try changing the script execution order so that your script executes before everything else. You likely have a conflict with the update order.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #10 on: April 22, 2014, 01:38:29 PM »
Mhm, I tried that and put it above everything else (set it to -32000) and nothing changed.
(I didn't forget to hit "Apply"!)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #11 on: April 23, 2014, 05:20:39 AM »
Then I am back to "I don't know".

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Show UILabel/Sprite above Player?
« Reply #12 on: April 23, 2014, 08:05:36 AM »
Mhm, that's not the answer I was hoping for :P

Alright. If you should have an idea, would be great if you could post it!

Thanks for the support, though! :)