Author Topic: Best way to anchor a widget to an object in the scene?  (Read 3989 times)

laurentl

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 188
    • View Profile
    • McDROID
Best way to anchor a widget to an object in the scene?
« on: June 10, 2013, 01:53:28 PM »
I see that anchor only attach to camera, panels and widgets. Is there a script to attach to a 3D object so that the health bar follows the object?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Best way to anchor a widget to an object in the scene?
« Reply #1 on: June 10, 2013, 02:42:02 PM »
No, that's what UIFollowTarget does from the HUDText add-on. Also it's just 3 lines of code anyway.

laurentl

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 188
    • View Profile
    • McDROID
Re: Best way to anchor a widget to an object in the scene?
« Reply #2 on: June 10, 2013, 02:47:16 PM »
I'm actually using it for context menu :)

I tried this but the UI gets sent really far.

  1.         void LateUpdate()
  2.         {
  3.                 transform.position = cam.WorldToScreenPoint (target.position);
  4.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Best way to anchor a widget to an object in the scene?
« Reply #3 on: June 10, 2013, 11:23:19 PM »
That's because WorldToScreenPoint gives you screen space coordinates, and then you just go and assign them to transform.position which is in world space coordinates.