Welcome,
Guest
. Please
login
or
register
.
May 05, 2026, 11:03:32 AM
Home
Help
Search
Login
Register
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
UI elements being placed outside of the camera's FoV
« previous
next »
Print
Pages: [
1
]
Author
Topic: UI elements being placed outside of the camera's FoV (Read 5304 times)
darthbator
Guest
UI elements being placed outside of the camera's FoV
«
on:
July 30, 2012, 04:59:11 AM »
So I've recently been trying to get the hang of NGUI but I am getting kinda hung up trying to get GUI elements to follow gameObjects around. I went ahead and checked the stickied health meter post to get the general idea of how something like that would work but I still haven't had any luck.
First I created a new simple 2D GUI. I then added a widget label. I then wrote the following script and attached it to one of my actors in the scene, and rigged the label object to labelPos. I figured I could use the position from the object and then go worldspaceObject.position-> gameCam.viewportSpace.position -> UIcam.worldspace.position like I saw in the health bar thread. However I don't think going from WorldtoViewportPoint to ViewporttoWorldPoint is working the right way. All my UI elements seem to be getting positioned outside of the field of view of my ui camera. Below is the script I am using.
using
UnityEngine
;
using
System.Collections
;
public
class
positionStuff
:
MonoBehaviour
{
public
Transform labelPos
;
private
Camera uiCam
;
private
Vector3 actorPos
;
private
Vector3 labelDest
;
void
Start
(
)
{
uiCam
=
NGUITools
.
FindCameraForLayer
(
(
int
)
Layers
.
Index
.
UI
)
;
}
void
LateUpdate
(
)
{
actorPos
=
Camera
.
main
.
WorldToViewportPoint
(
transform
.
position
)
;
labelDest
=
uiCam
.
ViewportToWorldPoint
(
actorPos
)
;
labelDest
.
z
=
0
;
labelPos
.
localPosition
=
labelDest
;
}
}
Logged
ArenMook
Administrator
Hero Member
Thank You
-Given: 337
-Receive: 1171
Posts: 22,128
Toronto, Canada
Re: UI elements being placed outside of the camera's FoV
«
Reply #1 on:
July 30, 2012, 06:18:54 AM »
uiCam.ViewportToWorldPoint gives you world coordinates, but you set labelPos.localPosition, which is local, not world. Change the last part to be labelPos.position, and it will work.
Logged
darthbator
Guest
Re: UI elements being placed outside of the camera's FoV
«
Reply #2 on:
July 30, 2012, 06:42:31 PM »
It doesn't really seem to matter how I address that (in local or world space) the point value coming out of
uiCam.ViewportToWorldPoint(actorPos)
is (999.8, 999.9, 0.0) (after I 0 the z value). Which seems to always put it outside of the UI camera's FoV....
I was originally using world space positioning but tried moving it local space but that didn't seem to work either.
Logged
ArenMook
Administrator
Hero Member
Thank You
-Given: 337
-Receive: 1171
Posts: 22,128
Toronto, Canada
Re: UI elements being placed outside of the camera's FoV
«
Reply #3 on:
July 30, 2012, 08:01:21 PM »
Make sure the cameras are correct. That's usually the most common cause of this not working properly. Is Camera.main really your game camera? Is uiCam really your only UI camera?
Logged
darthbator
Guest
Re: UI elements being placed outside of the camera's FoV
«
Reply #4 on:
July 30, 2012, 08:45:21 PM »
I have only 2 camera's in the scene. One on my UI layer (the camera that came with the "simple 2d UI" I added to the scene and then my Main Camera in the game that is wearing the MainCamera tag...
I'm having a really rough time figuring this out. Does it matter that I am moving the widget around on the panel rather then moving around the panel?
Logged
ArenMook
Administrator
Hero Member
Thank You
-Given: 337
-Receive: 1171
Posts: 22,128
Toronto, Canada
Re: UI elements being placed outside of the camera's FoV
«
Reply #5 on:
July 31, 2012, 12:38:41 AM »
HUDText package has a script called UIFollowTarget. It does all this for you. Just place it on the widget (or whatever UI object you want), specify a 3D world target to follow, and you're done.
Snipplet from its update function is trivial:
Vector3 pos
=
mGameCamera
.
WorldToViewportPoint
(
target
.
position
)
;
transform
.
position
=
mUICamera
.
ViewportToWorldPoint
(
pos
)
;
Logged
Print
Pages: [
1
]
« previous
next »
Tasharen Entertainment Forum
»
Support
»
NGUI 3 Support
»
UI elements being placed outside of the camera's FoV