Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is the difference between local and world coordinates?

Discussion in 'Scripting' started by xAmrxxxx, Mar 2, 2016.

  1. xAmrxxxx

    xAmrxxxx

    Joined:
    Jan 5, 2016
    Posts:
    56
    Function InverseTransformPoint(Vector3) changes vector3 from world to local coordinates i wanna know the difference between the. help would be appreciated
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    When you have GameObjects in GameObject, you can move those GameObjects around relative to the parent.

    Local position/rotation/scale, is that transformation relative to its parent.

    Global is the combination of a GameObject's local transformation with all of its parents, to the root of the scene.
     
  3. xAmrxxxx

    xAmrxxxx

    Joined:
    Jan 5, 2016
    Posts:
    56
    So local coordinates are not 2D coordinates ?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    well, they can be, if you're dealing with 2d.

    But being local has nothing to directly to do with if it's 2d or not. 3d has both local and global. And 2d has both local and global.

    It all has to do with nesting GameObjects in one one another (3d or 2d).


    Imagine you're in a car. You have a position and rotation relative to in the car. You're in the driver seat facing out the window. BUT, as the car drives around, and turns, your position on earth is changing, as well as your cardinal facing direction, but your position within the car has not changed.

    Same goes with the GameObjects. If you nest a GameObject in another, and then move the parent GameObject around, the child GameObject moves with it. It's local position is that inside its parent, where as the global is its position in the scene.
     
  5. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    localPosition is the object's position relative to its parent. Move the parent and the child follows, and the child's localPosition will not change. The position (world) is the place it is in the world (regardless of parent position).
     
    Last edited: Mar 3, 2016
    intizarali, Mohikann and nogxx like this.
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    More generally, you always need a frame of reference (usually defined with 3 axis and an origin) when using numbers to precisely encode positions. It gives physical meaning to the numbers.

    In Unity, positions are encoded using the Vector3 struct, which is basically 3 float numbers. But three numbers are not enough to specify a position, you need also to indicate in which space (or frame of reference ) a position is expressed.

    The local space is defined relative the object: The origin is the GameObject center and the axis are the direction of the object, right, up and forward.

    The world space is relative to ...the world: The origin is the center of the world (is up to you to give meaning to that) and the axis are right, up and forward, which is often mapped to east, up and north.

    There are plenty of spaces in unity, local space, world space, camera space, screen space, viewport space, ...
    A position expressed in one space can be expressed into another space using a transformation. Which is often done by matrix multiplications or using the equivalent helper functions.

    Then the whole game consists of knowing the right transformation from one space to another.

    For example, if you want to know the position expressed in the local space in the world-space, you use the localToWorldMatrix. If you want it in the other direction, you use the worldToLocalMatrix ( the inverse matrix).