Unity is one of the most popular game engines for 3D game development. To create immersive 3D worlds, it’s essential to understand how Unity’s 3D space works. In this blog, we’ll cover the fundamentals of 3D space in Unity and how you can interact with objects within it.
1. Understanding Unity’s Coordinate System
Unity uses a left-handed Cartesian coordinate system for defining positions in 3D space. This system helps us place, move, and rotate objects within the scene.
X-axis: Moves objects left to right. Positive values move right.
Y-axis: Moves objects up and down. Positive values move upward.
Z-axis: Moves objects forward and backward. Positive values move forward (into the scene).
2. Transform Component: Position, Rotation, and Scale
Every GameObject in Unity has a Transform component, which defines its position, rotation, and scale in the world.
Position: Defines where the object is located in 3D space using X, Y, and Z coordinates.
Rotation: Controls the object’s orientation, and you can modify it using Euler angles or quaternions for more complex rotations.
Scale: Defines the size of the object along each axis. For example, a scale of (1, 1, 1) means the object is at its original size, while (2, 1, 1) means it’s twice as wide but the same height and depth.
3. Local vs. Global Transformations
In Unity, transformations can be applied in local space or global space.
Global space refers to the world’s coordinate system. For example, moving an object to (0, 1, 0) places it one unit above the world’s origin.
Local space refers to transformations relative to the object’s parent (if it has one). When moving an object locally, its position is updated based on the parent’s transform.
4. GameObject Hierarchy
Unity organizes GameObjects in a parent-child hierarchy. Child objects inherit the transformations of their parent.
- When you move or rotate a parent object, all child objects move and rotate along with it. This is especially useful for organizing complex objects, such as a car with separate wheels and doors.
5. Camera and Perspective in Unity
The Camera is the view through which players experience the game. Unity uses two types of cameras:
Perspective Camera: This type of camera gives a 3D perspective, where objects farther away appear smaller, mimicking real-world depth perception.
Orthographic Camera: Objects appear the same size regardless of their distance from the camera, which is useful for 2D games or certain stylistic 3D scenes.
Cameras also have clipping planes, which define the range of objects visible to the camera. The near clipping plane sets the closest distance, and the far clipping plane sets the farthest distance the camera will render.
6. Units in Unity: Scale and Distance
Unity doesn’t assume any specific unit of measurement. By convention, 1 Unity unit = 1 meter, but this is arbitrary and can be adjusted based on your game’s requirements. For example, if you're developing a game with miniatures, one unit could represent a smaller measurement.
7. Lighting in 3D Space
Lights in Unity are essential for creating realistic scenes. There are several types of lights that affect how objects appear:
Directional Light: Simulates sunlight. It illuminates all objects equally, regardless of their position.
Point Light: A light source that emits light in all directions from a single point, like a lightbulb.
Spotlight: Emits light in a cone shape, similar to a flashlight.
Lighting also affects how shadows are cast, adding depth and realism to your scene.
8. Physics and Colliders
Physics in Unity’s 3D space is handled by the Rigidbody and Collider components.
Rigidbody: Adds physics-based movement to GameObjects. With a Rigidbody, objects respond to forces such as gravity or collisions.
Collider: Defines the physical shape of an object for purposes of collision detection. Common types of colliders include Box Collider, Sphere Collider, and Mesh Collider.
9. Rendering in 3D Space: Layers and Sorting
Unity uses Layers and Sorting Layers to manage which objects are rendered and in what order. For example, objects can be assigned to specific layers (e.g., "Player," "Environment") to control how they interact with other objects and how they are rendered.
Sorting Layers are important for controlling the render order of transparent objects. They allow you to specify which objects appear in front of or behind others in 3D space.
10. Gizmos and Debugging 3D Space
Unity provides Gizmos to help visualize certain aspects of 3D space during development. For instance, you can visualize the range of a light or the size of a collider using Gizmos.
Additionally, the Gizmos toolbar in the Scene view allows you to toggle visibility for different components, making it easier to debug and fine-tune your scene.
Conclusion
Understanding these basic principles of 3D space in Unity is crucial for building immersive and interactive worlds. Whether you're moving objects, setting up cameras, or managing lights and physics, mastering these fundamentals will help you take full control of your 3D scenes.
Note: cover photo collected from FreyaHalmar