Author Topic: How to convert the world position to a position on the screen?  (Read 11700 times)

Syberex

  • Guest
I'm working on gunsight. Did
  1. Vector3 viewpos = gameCamera.WorldToViewportPoint(target.position);
  2. Vector3 screenpos = UICamera.currentCamera.ViewportToScreenPoint(viewpos);
  3. gunSightPanel.localPosition = new Vector3 (screenpos.x, screenpos.y, 0);
  4.  
... at 800*480 works, and by 480*320 is not correctly positioned sight.
(UIRoot is not automatic, manual height = 480. UICamera is ortho, size =1.)
How to fix this?

Sorry for bad english  ::)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to convert the world position to a position on the screen?
« Reply #1 on: May 03, 2012, 08:23:33 PM »
UICamera.currentCamera only works in the event callbacks such as OnDrag, OnClick, etc. Outside of those callbacks you need to explicitly specify which camera to use.

Syberex

  • Guest
Re: How to convert the world position to a position on the screen?
« Reply #2 on: May 03, 2012, 08:45:12 PM »
This is not fixed bag  :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to convert the world position to a position on the screen?
« Reply #3 on: May 03, 2012, 08:57:50 PM »
Since you mentioned your UIRoot is not automatic, you need to take your size into account:

  1. float multiplier = uiroot.manualHeight / Screen.height;
  2. gunSightPanel.localPosition = new Vector3 (screenpos.x * multiplier, screenpos.y * multiplier, 0);

Syberex

  • Guest
Re: How to convert the world position to a position on the screen?
« Reply #4 on: May 04, 2012, 10:19:44 AM »
  1. float multiplier = (float) uiroot.manualHeight / (float) Screen.height;
  2. gunSightPanel.localPosition = new Vector3 (screenpos.x * multiplier, screenpos.y * multiplier, 0);
  3.  

Thanks ArenMook!