Unity says a little bit about the transformations of the various coordinates. Like Worldtoscreenpoint.

Source: Internet
Author: User

Previously wrote an essay on the display of the name on the head of an object.

The difficult point of estimation is the conversion of various coordinates.

Here in detail (on my level, how can be detailed ~ ~ ~) to explain. The sum of ......

In another way, raise a chestnut.

It is also possible to display the UI on top of an object or a part of the body and follow the movement of the object to change position.

Put the chestnuts on the first side:

Public Transform boy;//The protagonist of the handsome
Public Transform ui;//to the protagonist Stalker UI June
Camera cam;//Main camera
void Update ()
{

Ui.localposition = Cam. Worldtoscreenpoint (boy.position)-New Vector3 (SCREEN.WIDTH/2, SCREEN.HEIGHT/2, 0);
}

So long a string of code nasty dead. Listen to me slowly

The first is to seek the localposition of the UI.

We first convert the world coordinates of the protagonist object into a screen coordinate cam. Worldtoscreenpoint (boy.position). Then this value becomes the coordinates that the protagonist displays on the screen, which corresponds to the screen.width, yes, they are a dimensional.

However, the coordinates of our UI are relative to the UI root.

How to do it ~

Take a look at this little piece of Ngui I intercepted:

, the child UI underneath the UI root (0,0,0) is at the very center of the screen. and the screen coordinate system (0,0,0) ~ in the lower left corner of the screen, so between them, the difference between the width and height of half the screen.

The truth is only one, using the resulting object's screen coordinates minus half the screen width and height, you can get the relative coordinates of the UI. ~~~~~~~~~~~~~~~~~~~~~~~~

So, what did the chestnut mean before:

Vector3 pos = Cam. Worldtoviewportpoint (boy.position); UI.transform.position = Uicam.viewporttoworldpoint (POS);
The first sentence is to get the protagonist's viewport coordinates relative to the window first.
The second sentence is to convert the resulting viewport position into world coordinates.
Nani?

Amount, viewport coordinates are relative to the camera. The lower left of the camera is (0,0);

But, look closely at the two viewport coordinates is relative to which camera's ~

The idea is this: the world coordinates of the object corresponding to the world Camera viewport coordinates to find, and then the coordinates to the UI camera, and then find out this UI camera location of the viewport coordinates in the 3D world position.

Kill me. ~~~~~~~~~~~~~~~~~~~~~~~~~ messy into a firework.



Unity says a little bit about the transformations of the various coordinates. Like Worldtoscreenpoint.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.