Unity Learning Note-local coordinates to world coordinates

Source: Internet
Author: User

First of all, I am a programmer (rookie programmer), with C # development, in the development process will encounter some problems, at that time, but in the encounter may also be committed, so inspired me to do such a study note series, one is to be able to urge themselves to summarize learning, Secondly, you can learn to grow up with like-minded friends on the road of Change, I write the wrong place also hope that friends can point out, there are any questions can leave a message, we can together think together progress.
Last week when I was writing the UI component, I encountered a problem--ui the transformation between the coordinates of the interface and the world coordinates. Today I will talk about this problem first, in fact, the main is two methods. We're going to talk about local coordinates to world coordinates today.

From its own coordinates to world coordinates

  PublicTransform Testtra;  PublicTransform Parenttra;    Vector3 Localpos;    Vector3 Worldpos; voidAwake () {Localpos=testtra.localposition; Worldpos=Parenttra.transformpoint (Localpos); }    voidUpdate () {if(Input.getkeydown (keycode.a)) {Debug.Log ("Localpos:"+ Localpos +"Worldpos:"+Worldpos); }    }

The core use of the method is transform. Transformpoint ()

The return value of this method is the world coordinate of the Vector3 type, transform is the relative object, the parentheses are relative to the local coordinates of this transform, for example, my position now, know my relative to my neighbor's coordinates: POS1, And my relative to the cell coordinates Pos2, converted into world coordinates, the result is the same, how to do? One way to say this is: my neighbor. Transformpoint (POS1) to return to my world coordinates, there is another: community. Transformpoint (POS2) can also get my world coordinates.

Look at me wearing pieces out of a red ball and a plane

Look at the coordinates on the right, I put the ball in the plane, the red ball position relative to the plane is (-2, 0, 0), the position of the white plane is (2, 0, 0)
After looking at the last book press the local coordinates of the A output are the world coordinates are (-2, 0, 0) and (0, 0, 0) are correct. Local coordinates to world coordinates that's how it's used.

Next time I'm talking about how the world coordinates are converted to the cost coordinates.

Unity has world coordinates, local coordinates (localposition), UI coordinates (which I don't seem to be using), screen coordinates, and so on, before a project needs to consider the conversion of world coordinates to local coordinates, and then, after some attempts, find a way to compare trickery.

First, world coordinates refer to the coordinates of an object in the scene, and when an object has no parent, its position is the position,rotation of the world's coordinates; The local coordinates are the coordinates of the object relative to its parent (I have not studied the parent object and the parent object), This relative coordinate is calculated by the parent object itself as an axis, and is not necessarily associated with world coordinates. For objects that do not have a parent object, you can assume that there is no local coordinate. It is also important to note that when an object has a parent object, the position in its Inspector Bar transform is actually localposition, the local coordinates.

So how do you make the two coordinates interchangeable? First of all, the local coordinates to the world coordinates already have predecessors proposed one method: Click the Open link. The core use of the method is transform. Transformpoint ().

I then thought about how to convert the world coordinates to local coordinates (of course relative to a "parent" object) and found no correlation function. Later, a shot of the brain seeds: Sub-objects displayed in the Inspector interface is not the local coordinates it! So here's how to do this: Create an empty gameobject at the world coordinates that need to be converted, and set its parent to the "Father" object that you need to convert to the corresponding local coordinates. Then the empty gameobject.transform.localPosition is the request.

Wait, why is localposition and not position? Because transform defines both of these parameters, it represents its local and world coordinates, respectively.

So...... If you want to get the world coordinates of a sub-object m, the direct m.transform.position is done!!!!!!

Of course, if you only have a local coordinate you want to convert to world coordinates, and do not bother to create an empty gameobject, then use the previous method of the predecessor mentioned ~ ~

Unity Learning Note-local coordinates to world coordinates

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.