Support => NGUI 3 Support => Topic started by: hummada23 on January 24, 2015, 06:38:37 PM
Title: WorldToScreenPoint and NGUI
Post by: hummada23 on January 24, 2015, 06:38:37 PM
Hi,
I have an orthographic camera that films the game world, and a gui camera with a UIRoot that uses fixed scaling. I'm trying to get the screen coordinates of an object in the world so that I can draw a gui component over it.
There are lots of similar questions in this forum, but so far I haven't been able to get any of the suggested answers to work. Any help would be appreciated! Here is the code I'm currently using:
Vector3 v = cameraWorld.camera.WorldToScreenPoint(myWorldObject.transform.localPosition);
Title: Re: WorldToScreenPoint and NGUI
Post by: hummada23 on January 27, 2015, 07:08:16 PM
Is this even possible to do with an orthographic camera, or do you need a ray cast? I'm reluctant to use a ray cast, as I'm trying to draw a GUI element and it's a bit expensive to cast a ray every frame just to work out where to place the widget.
Title: Re: WorldToScreenPoint and NGUI
Post by: ArenMook on January 28, 2015, 02:19:14 AM
Look at what you're doing:
Vector3 v = cameraWorld.camera.WorldToScreenPoint(myWorldObject.transform.localPosition);
You should be using 'position', not 'localPosition'. Not sure what you're doing with the manual height / screen height either... why not just use UIRoot.pixelSizeAdjustment property, or the UIRoot.GetPixelSizeAdjustment(gameObject) function?
Lastly, you make the same mistake again in the end:
P.S. NGUI can do all this math for you. Just use transform.OverlayPosition.
Title: Re: WorldToScreenPoint and NGUI
Post by: hummada23 on January 28, 2015, 06:20:46 AM
Fantastic, thanks!
OverlayPosition does what I need, and by looking at the source code for NGUIMath I was able to understand what needed to be done (I also needed to upgrade to the latest version of NGUI, as it looks like OverlayPosition isn't in the earlier iterations of version 3).
The result kind of jitters a bit when I move the camera, but it solves a big problem and now I have something good to work with. Thanks again!