Author Topic: [SOLVED] How to overlay a GameObject position at an NGUI Widget position  (Read 4538 times)

bach

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 6
    • View Profile
I am pretty much looking for the opposite of NGUIMath.OverlayPosition

I want to position a GameObject viewed by World Camera
at the position of the NGUI Widget viewed by UI Camera

I've tried all sort of combinations without much luck:

  1. Vector3 pos = nguiCamera.ViewportToWorldPoint(nguiWidget.transform.localPosition);
  2. pos = Camera.main.WorldToViewportPoint(pos);

thanks
« Last Edit: April 22, 2015, 04:10:34 AM by bach »

cuidaguang

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 3
  • Posts: 5
    • View Profile
Re: How to overlay a GameObject position at an NGUI Widget position
« Reply #1 on: April 20, 2015, 04:19:27 AM »
Vector3 world_pos = Camera.main.WorldToScreenPoint (position);
pos.z= 0;
Vector3 ui_pos = UICamera.mainCamera.ScreenToWorldPoint(world_pos);
gameObject.transform.position = uipos;

bach

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 1
  • Posts: 6
    • View Profile
Re: How to overlay a GameObject position at an NGUI Widget position
« Reply #2 on: April 22, 2015, 04:10:04 AM »
Vector3 world_pos = Camera.main.WorldToScreenPoint (position);
pos.z= 0;
Vector3 ui_pos = UICamera.mainCamera.ScreenToWorldPoint(world_pos);
gameObject.transform.position = uipos;

Thanks for pointing me in the right direction!

My code ended up as below:
  1. Vector3 worldPos = UICamera.mainCamera.WorldToViewportPoint(nguiSprite.transform.position);
  2. Vector3 uiPos = Camera.main.ViewportToWorldPoint(worldPos);
  3. gameObject.transform.position = uiPos;
  4.