Author Topic: Massive widget translation  (Read 3045 times)

Madgodz

  • Guest
Massive widget translation
« 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.
« Last Edit: August 08, 2012, 11:44:06 AM by Madgodz »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Massive widget translation
« Reply #1 on: August 08, 2012, 01:44:00 PM »
Viewport coordinates are in range of -1 to 1. Assuming "m_Pos" is in screen coordinates, you should use ScreenToWorldPoint instead.

Madgodz

  • Guest
Re: Massive widget translation
« Reply #2 on: August 08, 2012, 03:09:33 PM »
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;
      }
   }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Massive widget translation
« Reply #3 on: August 08, 2012, 03:50:42 PM »
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.

Madgodz

  • Guest
Re: Massive widget translation
« Reply #4 on: August 08, 2012, 04:51:42 PM »
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).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Massive widget translation
« Reply #5 on: August 08, 2012, 05:10:16 PM »
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.