Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started by: Madgodz on August 08, 2012, 11:38:57 AM
-
Using the Workshop project I tried to implement overhead nametags and health bars. It works great except for one issue. When I call this function in LateUpdate():
m_Pos = m_UICam.ViewportToWorldPoint(m_Pos);
The position moves the widget to ~45,000 on the X axis and same magnitude on Y axis. If you look at the WorldToLocal matrix values for the nametag widget something seems to be causing [1,1], [2,2], and [3,3] (basically scale component) to be huge numbers ~500. We use autoscaling and when the scale vector is procedurally generated for the UIRoot, The value is very small and basically matched the LocalToWorld matrix scale values for the nametag widget (~.00186).
Edit: It is the localPosition value of the nametag widget that contains the massive X and Y values.
-
Viewport coordinates are in range of -1 to 1. Assuming "m_Pos" is in screen coordinates, you should use ScreenToWorldPoint instead.
-
Here's the entire lateUpdate function from your (assuming) UnitHUD.cs script:
void LateUpdate()
{
if (target == null) { Destroy(gameObject); return; }
mPos = mGameCam.WorldToViewportPoint(target.position);
bool visible = (mPos.z > 0f && mPos.x > 0f && mPos.x < 1f && mPos.y > 0f && mPos.y < 1f);
if (mVisible != visible)
{
mVisible = visible;
UIWidget[] widgets = gameObject.GetComponentsInChildren<UIWidget>();
foreach (UIWidget w in widgets) w.enabled = mVisible;
}
if (mVisible)
{
mPos = mUICam.ViewportToWorldPoint(mPos);
mPos.z = 0f;
mTrans.position = mPos;
}
}
-
Yeah that script is fine, but you need to make sure that it uses the proper cameras. Make sure the game camera and UI cameras are what you think they are.
-
Yeah that script is fine, but you need to make sure that it uses the proper cameras. Make sure the game camera and UI cameras are what you think they are.
Hmm. I think I'm confused. When does your above comment about ScreenToWorldPoint come into play? Do I change the UnitHUD script or do I make the change elsewhere?
Also my cameras are definitely correct (I add and validate tags to make sure).
-
No, my original comment was due to me thinking you were feeding screen coordinates to it, but i see they're actually viewport coordinates, so it's fine. If your cameras are correct, is "target.position" correct? It should be world-space position of whatever the script is watching -- for example the game unit.