Author Topic: Position UILabel where 3D character is stood  (Read 10222 times)

Cloun

  • Guest
Position UILabel where 3D character is stood
« on: October 20, 2012, 07:56:39 PM »
I want to put a 2D UILabel at the position of the character in a 3D world.

I have the following code but the label seems to be offset from the character by different amounts depending where the character is - it's never at the position of the characters feet.  I've tried anchoring my label to the center and the bottom left.

Vector3 pos = gameCam.WorldToViewportPoint(player.transform.position);
thisLabel.transform.position = pos;

or

Vector3 pos = gameCam.WorldToScreenPoint(player.transform.position);
thisLabel.transform.localPosition = pos;


Any ideas?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Position UILabel where 3D character is stood
« Reply #1 on: October 20, 2012, 09:15:24 PM »
If "target" is the transform referencing your 3D object, and this script is attached to your UI object, then...
  1. Vector3 pos = mGameCamera.WorldToViewportPoint(target.position);
  2. transform.position = mUICamera.ViewportToWorldPoint(pos);
HUDText package on the asset store has UIFollowTarget script that does this, complete with disabling the widgets if the 3D object isn't visible and ensuring that the widgets are pixel-perfect.

Cloun

  • Guest
Re: Position UILabel where 3D character is stood
« Reply #2 on: October 21, 2012, 04:41:21 AM »
That works perfectly, thanks :)

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Position UILabel where 3D character is stood
« Reply #3 on: April 24, 2014, 08:21:01 AM »
Sorry to necro an old thread, but I'm actually trying to use this method to go the other way, and failing miserably.

I'm trying to match a world object to be at the same position on screen as the UI object, but I'm getting nowhere with this.

This is what I have so far, but it's not working at all.

  1. myTarPos = myTarget.transform.position;
  2.         tarPosition = cam.ViewportToWorldPoint(myTarPos);
  3.         transform.position = tarPosition;

"cam" is the NGUI Camera
"myTarPos" it the UI element I'm trying to match in world space.

I hope it doesn't matter, but my NGUI cam size is "1", while my Camera.main is "4.4".
« Last Edit: April 24, 2014, 08:30:35 AM by Rick74 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Position UILabel where 3D character is stood
« Reply #4 on: April 25, 2014, 05:31:14 AM »
It's hard to go the other way, because you are effectively adding a dimension. UI is in 2D. The world is in 3D. You can't simply go from 2D to 3D as one dimension is missing.

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Position UILabel where 3D character is stood
« Reply #5 on: April 25, 2014, 09:51:24 AM »
not sure if it matters but both camera are orthographic, and if I could just match the position 2d I could create the Z offset myself.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Position UILabel where 3D character is stood
« Reply #6 on: April 25, 2014, 10:24:23 AM »
In your case you are taking a world position, then using ViewportToWorld conversion. It's already in world. What are you trying to do? I think you mean to convert world to viewport using one camera, then viewport to world using the other camera. That would work.

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Position UILabel where 3D character is stood
« Reply #7 on: April 25, 2014, 09:02:37 PM »
Thanks!  I tried that, and it worked perfectly!
« Last Edit: April 25, 2014, 09:10:38 PM by Rick74 »