Author Topic: Transform.OverlayPosition sets non-zero z value  (Read 3430 times)

jmorhart

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Transform.OverlayPosition sets non-zero z value
« on: February 03, 2015, 03:26:56 PM »
I would like to overlay a label on top of a 3D game object. I have attached a script to the object that does the following in the update loop.

  1. public Transform labelTransform;
  2.  
  3. void Update()
  4. {
  5.     labelTransform.OverlayPosition(transform.position, Camera.main, UICamera.mainCamera);
  6. }
  7.  

I can see in the editor that this is setting the label transform's X and Y positions correctly, but the Z position varies from -10,000 to 10,000 so the label doesn't get rendered. How can I make the Z position always be 0?

Thanks!

jmorhart

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Transform.OverlayPosition sets non-zero z value
« Reply #1 on: February 03, 2015, 04:35:37 PM »
I've figured out a different way to do what I'd like. I used NGUIMath.WorldToLocalPoint and then manually set the z value of Vector3 returned from that to 0. Then set the widget's transform.localPosition to that modified Vector3.

  1. void Update()
  2. {
  3.     Vector3 overlay = NGUIMath.WorldToLocalPoint(transform.position, Camera.main, UICamera.mainCamera, labelTransform);
  4.     overlay.z = 0.0f;
  5.     labelTransform.localPosition = overlay;
  6. }
  7.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Transform.OverlayPosition sets non-zero z value
« Reply #2 on: February 05, 2015, 08:56:40 AM »
You were passing the wrong vector3 to your OverlayPosition function. The position was supposed to be your 3D object's position. This script should also have been on the label, not on the 3D object. But if your current approach works, it's fine, stick with it.