Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Cloun on October 20, 2012, 07:56:39 PM

Title: Position UILabel where 3D character is stood
Post by: Cloun 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
Title: Re: Position UILabel where 3D character is stood
Post by: ArenMook 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.
Title: Re: Position UILabel where 3D character is stood
Post by: Cloun on October 21, 2012, 04:41:21 AM
That works perfectly, thanks :)
Title: Re: Position UILabel where 3D character is stood
Post by: Rick74 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".
Title: Re: Position UILabel where 3D character is stood
Post by: ArenMook 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.
Title: Re: Position UILabel where 3D character is stood
Post by: Rick74 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.

Title: Re: Position UILabel where 3D character is stood
Post by: ArenMook 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.
Title: Re: Position UILabel where 3D character is stood
Post by: Rick74 on April 25, 2014, 09:02:37 PM
Thanks!  I tried that, and it worked perfectly!