0
0
Unityframework~5 mins

3D coordinate system in Unity

Choose your learning style9 modes available
Introduction

A 3D coordinate system helps us find and place objects in a 3D space using three numbers: X, Y, and Z.

When placing a character or object in a 3D game world.
When moving an object up, down, left, right, forward, or backward.
When rotating or scaling objects based on their position in space.
When detecting where a player or camera is located in the scene.
When creating animations that move objects through 3D space.
Syntax
Unity
Vector3 position = new Vector3(x, y, z);

Vector3 is a Unity type that holds three float numbers for X, Y, and Z.

You can use this to set or get an object's position in 3D space.

Examples
This creates a point at X=1, Y=2, Z=3 in 3D space.
Unity
Vector3 point = new Vector3(1, 2, 3);
This moves the current object 5 units up on the Y axis.
Unity
transform.position = new Vector3(0, 5, 0);
This represents a direction pointing forward along the Z axis.
Unity
Vector3 forward = new Vector3(0, 0, 1);
Sample Program

This script moves the object to position (1, 2, 3) when the game starts and prints the position to the console.

Unity
using UnityEngine;

public class MoveObject : MonoBehaviour
{
    void Start()
    {
        // Set the object's position to (1, 2, 3)
        transform.position = new Vector3(1, 2, 3);
        Debug.Log($"Object position: {transform.position}");
    }
}
OutputSuccess
Important Notes

The X axis usually goes left and right.

The Y axis usually goes up and down.

The Z axis usually goes forward and backward.

Summary

A 3D coordinate system uses X, Y, and Z to locate points in space.

Unity uses Vector3 to work with these coordinates.

You can move objects by changing their transform.position using Vector3.