Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Syberex on May 03, 2012, 08:18:36 PM

Title: How to convert the world position to a position on the screen?
Post by: Syberex on May 03, 2012, 08:18:36 PM
I'm working on gunsight. Did
  1. Vector3 viewpos = gameCamera.WorldToViewportPoint(target.position);
  2. Vector3 screenpos = UICamera.currentCamera.ViewportToScreenPoint(viewpos);
  3. gunSightPanel.localPosition = new Vector3 (screenpos.x, screenpos.y, 0);
  4.  
... at 800*480 works, and by 480*320 is not correctly positioned sight.
(UIRoot is not automatic, manual height = 480. UICamera is ortho, size =1.)
How to fix this?

Sorry for bad english  ::)
Title: Re: How to convert the world position to a position on the screen?
Post by: ArenMook on May 03, 2012, 08:23:33 PM
UICamera.currentCamera only works in the event callbacks such as OnDrag, OnClick, etc. Outside of those callbacks you need to explicitly specify which camera to use.
Title: Re: How to convert the world position to a position on the screen?
Post by: Syberex on May 03, 2012, 08:45:12 PM
This is not fixed bag  :(
Title: Re: How to convert the world position to a position on the screen?
Post by: ArenMook on May 03, 2012, 08:57:50 PM
Since you mentioned your UIRoot is not automatic, you need to take your size into account:

  1. float multiplier = uiroot.manualHeight / Screen.height;
  2. gunSightPanel.localPosition = new Vector3 (screenpos.x * multiplier, screenpos.y * multiplier, 0);
Title: Re: How to convert the world position to a position on the screen?
Post by: Syberex on May 04, 2012, 10:19:44 AM
  1. float multiplier = (float) uiroot.manualHeight / (float) Screen.height;
  2. gunSightPanel.localPosition = new Vector3 (screenpos.x * multiplier, screenpos.y * multiplier, 0);
  3.  

Thanks ArenMook!