Muhammad Faizan Khan
Muhammad Faizan Khan

Reputation: 10551

local space vs global space vs object space in unity3d

I am watching this (game dev, space parenting & rotation) Sebastian Lague video and found that there are three spaces actually (not two) and these are world space, local space and object space.

  1. World space: The static space (0,0,0)
  2. Object space: related to the object space
  3. Local Space: related to the parent of the object

I am amazed that i didn't find the distinction between these two spaces(local and object) on official unity forms but actually it exists. My question is that why there is no Space.Local? I found that there are Space. Self and Space.World. Space.Self is refer to object space. I can move my object to object space using this

    void Update () {
        transform.Translate(new Vector3(0,0,1) * Time.deltaTime * 2,Space.Self);
    }

And i can move my object to world space using this

void Update () {
    transform.Translate(new Vector3(0,0,1) * Time.deltaTime * 2,Space.World);
}

but there is no support for local space that i could move the object to local space (means move the object related to its parent object). There is a fair distinction between Local and Object space but unity didn't consider it i guess or i am wrong.

Upvotes: 1

Views: 6718

Answers (2)

Ringleader William
Ringleader William

Reputation: 1

maybe you can use Transform.TransformDirection() method, like this:

    public class WhatIsTheLocalSpace : MonoBehaviour
{
    public Transform obt;
    public bool selfSpace = false;
    public bool worldSpace = false;
    public bool parentSpace = false;
    void Update() {
        if (Input.GetKey(KeyCode.D))
        {
            // move along self space
            if (selfSpace && !worldSpace && !parentSpace)
            {
                obt.Translate(Vector3.right*Time.deltaTime,Space.Self);
            }
            
            // move along world space
            if (!selfSpace && worldSpace && !parentSpace)
            {
                obt.Translate(Vector3.right*Time.deltaTime,Space.World);
            }

            // move along parent space
            if (!selfSpace && !worldSpace && parentSpace)
            {
                obt.Translate(obt.parent.TransformDirection(Vector3.right)*Time.deltaTime,Space.World);
            }
        }
    }
}

Upvotes: 0

Hellium
Hellium

Reputation: 7346

In the Unity's inspector, the position and rotation of the Transform component are relative to the parent (defined in the local space)

The axes represented by the gizmo handles are either the global ones (world space) or the object ones as pointed out by S. Lague in the video.

The local axes (axes of the parent Transform) are not represented when you select a gameobject, contrary to other 3D softwares (like Maya I think), and there is no C# function to translate in the local space but you can create one:

Vector3 right     = transform.parent.right;
Vector3 up        = transform.parent.up ;
Vector3 forward   = transform.parent.forward;
Vector3 direction = X * right + Y * up + Z * forward ;

transform.Translate(direction * Time.deltaTime, Space.World);

However, keep in mind that an object A will always have a position and rotation of (0, 0, 0), in object space because the latter is defined by the object itself. An object A can't be moved / rotated from itself.

Upvotes: 1

Related Questions