Author Topic: World to Screen Point  (Read 11754 times)

nathanj

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
World to Screen Point
« on: July 25, 2016, 06:56:25 PM »
Hi Again

I have another vague, please point me in the right direction please, question.

I'm trying to convert our player's transform position in the game world (3D) to a point on the UI screen (a 2D map that shows the overall terrain). I'm pretty sure that Unity's Camera.WorldToScreenPoint is the way to do this, however, I can not figure out how this would work.

The reason I am posting this here, and not on the Unity forum, is because I would like to also translate the position of specific places in our game to the map as well using icons that can be controlled through NGUI. That and we use NGUI for all of our UI design.

I spent yesterday trying to figure out a solution to this but only found abstract discussions that I honestly have no idea if they even achieve what I'm aiming for. If anyone could point in in the direction of how I would go about achieving this, or aware of any examples, I would be very grateful.

Nathan

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: World to Screen Point
« Reply #1 on: July 26, 2016, 10:34:04 PM »
Just use the widget.transform.OverlayPosition function. It does the math inside.

nathanj

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: World to Screen Point
« Reply #2 on: July 31, 2016, 08:03:24 PM »
Thanks Aren!

OK, So I've played around with this but am not exactly sure what "widget.transform.OverlayPosition function" is. Is this "NGUIMath.OverlayPosition"? I assume that is it and found an example on the forums that I've tried working with but am a bit perplexed as to how it works and what exactly it does.

Again, if there are examples of this type of functionality I would be happy to work with those, or if anyone can suggest how to proceed with this I would be very grateful.

Thanks again,
Nathan
  1. {
  2.  
  3.         public Vector3 worldPos;
  4.         public Camera worldCam;
  5.         public Camera uiCam;
  6.         public Transform relativeTo;
  7.         public Transform labelTransform;
  8.  
  9.         void Update ()
  10.         {
  11.                 Vector3 overlay = NGUIMath.WorldToLocalPoint (transform.position, Camera.main, UICamera.mainCamera, labelTransform);
  12.                 overlay.z = 0.0f;
  13.                 labelTransform.localPosition = overlay;
  14.         }
  15.         // Use this for initialization
  16.         void Start () {
  17.        
  18.         }
  19. }
  20.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: World to Screen Point
« Reply #3 on: August 01, 2016, 06:26:07 AM »
Overlay position sets the position of whatever you are calling it on to be directly above the world point. You need to call it on the widget's transform, exactly as I showed.
  1. widget.transform.OverlayPosition(worldObject.transform);