About World space and Local space

when I use several sample codes, I usually meet those ScreenToWorldPoint, WorldToScreenPoint, InverseTransformDirection etc…

these used many areas : camera or mouse move code.
and descriptions of these are convert space : from screen to local, from local to world or from world to local etc…

But I don’t know that difference and why I should use it.

plz teach me… T.T

Unity uses 3 different co-ordinate systems, World point system, Screen point system and view-port point system. These functions are used to find where the particular point will be in another point system.

To be clear, when you are giving a mouse input or touch input, it will be in screen point system. which has only x and y. to use this inside the game and set it to a particular object ( snap an object to mouse) takes certain work, you have to consider camera position, rotation, object depth and stuffs, ScreenToWorldPoint function makes the developer’s work simple. it converts the point to world co-ordinate jus by taking mouse position and z depth.

view port is the space to which the camera renders. sometime you can get the area occupied by an object on the view port and used it like a button(just an example), in case of orthographic it will be easy. In this care you have to convert the world point to and from viewport point. so you need to use WorldToViewportPoint and it alias function.

the image i have attached would show why the conversion is must [997-Untitled+drawing.pdf|997]

Local vs. world space: http://answers.unity3d.com/questions/31169/explanation-local-vs-global-space.html

Screen vs. world space: here is an example: http://answers.unity3d.com/questions/14404/create-an-object-in-screen-space-coordinate.html

A really short overview:

  • World Space: the absolute XYZ coordinates of all objects
  • Local Space: relative coordinates, in relation to a reference object. For example within an object hierarchy, the Transforms of all children are local to their parent
  • Screen space: an essentially twodimensional space, used to place objects directly at certain absolute pixel positions on your screen

The methods you found are used to convert from one coordinate representation to another. For example, convert the x/y position of a mouse to a direction from the camera position into the world space of the scene.

Also, google a bit for these keywords, you will find plenty of tutorials and explanations of each coordinate space.

// You can try this in Unity to see the value of Screen Space point, View Space point
// and World Sapce point.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
	/*
     * A screen space point is defined in pixels. The bottom-left of the screen
     * is ( 0, 0 ); the right-top is ( pixelWidth, pixelHeight ). The z position
     * is in world units from the camera.
     * 
     * A viewport space point is normalized and relative to the Camera. The 
     * bottom-left of the Camera is ( 0, 0 ); the top-right is ( 1, 1 ). The z 
     * position is in world unity from the camera.
     * 
     * A world space point is defined in global coordinates(for example, 
     * Transform.position).
     * 
     * There are a visual show:
     * http://blog.projectmw.net/unitys-screen-point-viewport-point-and-world-point
     * 
     * Local Space VS World Space:
     * http://answers.unity3d.com/questions/31169/explanation-local-vs-global-space.html
     * 
     * Reference:
     * https://docs.unity3d.com/ScriptReference/Camera.html
     */


	void OnGUI()
    {
        Camera c = Camera.main;
        Event e = Event.current;

        Vector2 mousePos = new Vector2();

        // Get mouse position in Screen space point
        mousePos.x = e.mousePosition.x;
        // The e.mousePosition.y is reverse
        mousePos.y = c.pixelHeight - e.mousePosition.y;

        Vector3 screenPoint = new Vector3( mousePos.x, mousePos.y, c.nearClipPlane );
        // View space point's z is the c.nearClipPlane
        Vector3 viewPoint = c.ScreenToViewportPoint( screenPoint );
        // When the Camera's rotation equal Vector3.zero, the World Space point's 
        // z is the c.transform.z + c.nearClipPlane
        Vector3 worldPoint = c.ScreenToWorldPoint( screenPoint );

        GUILayout.BeginArea( new Rect( 20, 20, 300, 120 ) );
        GUILayout.Label( "Screen pixels: " + c.pixelWidth + ":" + c.pixelHeight );
        GUILayout.Label( "Screen position: " + mousePos.ToString( "F2" ) );
        GUILayout.Label( "Viewpoint position: " + viewPoint.ToString( "F2" ) );
        GUILayout.Label( "World position: " + worldPoint.ToString( "F2" ) );
        GUILayout.EndArea();
    }
}

World Space is the XYZ on the main World, which you see in the upper right corner of the screen.

Local Space refers to the Objects XYZ which is entirely different with the World XYZ

if you use Rigidbody’s XYZ, it will use Local Space.
If you use the Transform, it will use the World Space.

If still unsure, I can redirect you to the Video Explanation.