Local and global coorlinate system

I’ve started to learn Unity and I’m not sure how local and global coordinates work. I have two scripts.

First (Responsible for character movement (A,W,S,D)):
public class Move : MonoBehaviour
{ // Start is called before the first frame update public float speed = 6.0f; private CharacterController _charController; public float gravity = -9.8f;
// Update is called once per frame

void Start()
{
    _charController = GetComponent<CharacterController>();
    
}
void Update()
{
    float deltaX = Input.GetAxis("Horizontal") * speed;
    float deltaZ = Input.GetAxis("Vertical") * speed;
    Vector3 movement = new Vector3(deltaX, 0, deltaZ);
    movement = Vector3.ClampMagnitude(movement, speed);
    movement.y = gravity;
    movement *= Time.deltaTime;
    //movement = transform.TransformDirection(movement);
    _charController.Move(movement);
} }

and second (mouse rotation):

public class MouseLook : MonoBehaviour
{ public enum RotationAxes {MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXAndY; public float sensitivityHor = 9.0f; public float sensitivityVert = 9.0f; private float _rotationX = 0; public float minimumVert = -900f; public float maximumVert = 900f;
void Start()
{
    Rigidbody body = GetComponent<Rigidbody>();
    if (body != null)  
   body.freezeRotation = true;
}


void Update()
{
    if (axes == RotationAxes.MouseX)
    {
        transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
    }
    else if (axes == RotationAxes.MouseY)
    {
        _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
        _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
        float rotationY = transform.localEulerAngles.y;
        transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
    }
    else
    {

        _rotationX  -= Input.GetAxis("Mouse Y") * sensitivityVert;
        _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
        float delta = Input.GetAxis("Mouse X") * sensitivityHor;  
        float rotationY = transform.localEulerAngles.y + delta;  
         transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);




    }
}
}

It is not clear to me exactly how the local and global coordinate systems work and interact here. What exactly happens when we translate from local to global coordinate system

movement = transform.TransformDirection(movement);

How my mouse rotation works here is also not entirely clear. As I see it, it’s in local coordinate system, so how does the code work so that when you turn the mouse, you will move straight ahead depending on where you turn it?

In summary, I don’t understand how translation from the local to global coordinate system works here, how these coordinate systems work by themselves, and why translate from one to the other at all? And how dependency works: which way you turn the mouse, that’s where you go straight.

The book I’m reading now doesn’t seem to have explained it very well, so I’m hoping for some help

@cortezz1ty - the distinction between local and global coordinates is crucial for understanding how objects relate to each other in 3D space.

Global Coordinate System: This is a reference point for all objects in your Unity scene. It’s essentially the world origin (0, 0, 0). The position of all game objects are specified in relation to this fixed point.

Local Coordinate System: This is unique to each game object. The origin (0, 0, 0) here is the object’s position. It enables us to position or move child objects relative to their parent.

When you use transform.position, you are interacting with the global coordinates. However, transform.localPosition refers to the local coordinates relative to the parent object.

//movement = transform.TransformDirection(movement);

This line, if uncommented, changes the direction of the movement from local to global coordinates. It doesn’t alter the magnitude of the vector, just the direction. It translates a direction from local space (your object’s space) to global space (world space). This can be useful when you want to move an object in its local direction but the effect to be seen in world space.

Your MouseLook script is also working on local coordinates. You can see this in the line where it says:

transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);

Here, _rotationX and rotationY are in local coordinates. This means that the rotation you’re applying is relative to the object’s current orientation, not the world’s. Hence, when you turn your mouse, you’re changing the local rotation of the character, not its rotation in global coordinates. As a result, moving straight ahead will now be in the direction of your new rotation, not the world’s.

In a nutshell, translating from local to global coordinates (and vice versa) allows us to take advantage of the hierarchical nature of game objects in Unity. Parent-child relationships can be easily managed, and it becomes simpler to control how objects move and rotate in relation to one another.

Lastly, about the mouse turning and direction of movement: When you rotate the mouse, the character changes its local rotation. When you move, you’re going “forward” in the direction that the character is currently facing, which is determined by its local rotation.

Let me know if this gets you closer to an answer and if it clarifies it for you.