简体   繁体   中英

Convert World Space GameObject Position to Screen Space Canvas UI Position

I have a GameObject that is in World Space. I also have UI GameObject that is in Canvas that is Screen Space that uses Camera.

How can I convert the position of World Space GameObject and assign it to the UI GameObject in Screen Space Canvas?

WorldToScreenPoint doesn't use help.

camera.WorldToScreenPoint(transform.position) should work. Keep in mind! 0,0 is bottom left. So to position a UI element, you need to invert Y

To do that, you can do this:

Vector3 screenPos = camera.WorldToScreenPoint(transform.position);
Vector3 uiPos = new Vector3(screenPos.x, Screen.height - screenPos.y, screenPos.z);

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM