Coordinate system transformation in unity

Source: Internet
Author: User
Https://li-kang.gitbooks.io/unity-concise-course/content/Summary/coordinate.html

coordinate system

In the 3D mathematical coordinate system, the following 3 coordinate systems are mentioned. World coordinate system: the coordinate system which is established by the universally accepted origin and axial direction. Object coordinate system: a coordinate system established by the position of the object and its own axis. Inertial coordinate system: the coordinate system established by the axis of object position and world coordinate system. World coordinate system The location of Unity's scene panel (0,0,0) is the origin of the world's coordinate system. The upper-right corner diagram indicates the axis of the world coordinate system.

Transform.position; The position of the object in the world coordinate system
Transform.forward;  The front of the object in the world coordinate system
Object coordinate systemWhen selecting an object, the position, Rotation, and scale under the transform of the Inspector refer to its position, rotation, and scaling in the parent object's coordinate system. When an object does not specify a parent object, its parent object is the world. That is, the position in the View panel transform is exactly where it is in the world coordinate system. Objects in the Unity engine hold data relative to the coordinate system of the parent object, so when the parent changes (move, rotate, scale), the sub-object changes synchronously.

[Screenshot transfrom Panel]

Transform.localposition; The position transform.localrotation in the coordinate system of the parent object
;//rotation transform.localscale in the coordinate system of the parent object
;    Zooming in the coordinate system of the parent object
Screen coordinate systemThe screen coordinate system is 2-dimensional. The screen coordinate system is based on the lower-left corner of the screen (0,0), the upper-right corner (Camera.pixelwidth, Camera.pixelheight).
Input.mouseposition; Get the position of the mouse in the screen coordinate system
Viewport coordinate systemThe viewport coordinate system is also used to reflect the position in the screen. The viewport coordinate system uses a percentage of the screen width and height, its lower-left corner (0,0), and the upper-right corner (. The viewport coordinate system is suitable for adapting between screens of different sizes. GUI coordinate systemSpecifically used with the GUI's coordinate system. and screen coordinates are upside-down. Its upper-left corner (0,0), lower right corner (Screen.width, Screen.hight) transformation of the coordinate systemGui\ Conversion between screens
The conversion between screen coordinates and GUI coordinates is the same as
Vector2 Transformscreenandgui (Vector2 point) {
  Vector2 result;

  X-Axis invariant
  result.x = point.x;
  The y-axis is the screen height minus the original y-coordinate
  result.y = Screen.hight-point.y;

  return result;
}
Screen \ Viewport \ conversion between Worlds
Screen Turn viewport
Camera.main.ScreenToViewportPoint (input.mouseposition); 

Viewport Turn screen
Camera.main.ViewportToScreenPoint (New Vector3 (0.25F, 0.1F, 0));

Screen to World
//After conversion z-axis unchanged
//If the input is Vector2, the z-axis size of the camera is the z-axis value
camera. Screentoworldpoint (input.mouseposition);

World Turn screen
camera. Worldtoscreenpoint (target.transform.position);

The viewport turns to the world
camera. Viewporttoworldpoint (New Vector3 (1, 1, Camera.nearclipplane));

World turn to viewport
camera. Worldtoviewportpoint (target.transform.position);
The transformation between the object and the world
The object turns to world coordinates
transform. Transformpoint (3, 4, 5); 

World Turn object coordinates
transform. Inversetransformpoint (3, 4, 5);

The direction of the object coordinate system goes to the world coordinate direction
transform. Transformdirection (Vector3.forward);

The direction of the world coordinate system goes to the object coordinate direction
transform. Inversetransformdirection (Vector3.forward);
Application Examples
//Determine whether the target is on the left side of the screen or the right using Unityengine; using System.Collections;
    public class Exampleclass:monobehaviour {public Transform target;

    Camera camera;
    void Start () {camera = getcomponent<camera> (); } void Update () {Vector3 Viewpos = camera.
        Worldtoviewportpoint (target.position);
        if (viewpos.x > 0.5F) print ("Target is in the right side!");
    else print ("Target is on the left side!"); }
}

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.