Conteúdo do Curso
Unity for Beginners
Unity for Beginners
1. Unity Introduction
2. Write your First Script
5. Polishing and Export your Game
Transform Component
The Transform component in Unity is like the GPS for a GameObject. It tells Unity where the GameObject is, how it's rotated, and how big it is in the game world.
Control the transform component with Script:
In Unity scripting, when you refer to transform within a script, you're essentially referring to the Transform component of the GameObject to which that script is attached. Let me break it down:
Inside your script, when you use the keyword transform
, you're referring to the Transform
component of the GameObject
to which the script is attached. This means you're accessing information about the position, rotation, and scale of that particular GameObject
.
For example, transform.position
would give you the current position of the GameObject.
So, whenever you see transform in a script, just think of it as a way to access and work with the Transform component of the GameObject that the script is controlling. It's a convenient way to interact with and manipulate the properties of GameObjects programmatically.
Move our object with the Transform
This line of code moves the GameObject that the script is attached to. Let's break it down:
transform
: This refers to the Transform component of the GameObject. The Transform component stores the GameObject's position, rotation, and scale;Translate
: This is a method of the Transform component that moves the GameObject;Vector2.up
: This is a built-in Unity constant that represents the direction "up" in the GameObject's local coordinate system;Space.Self
: This tells the Translate method to move the GameObject in its local coordinate system.
Note
Note: We can translate the player in the world coordinate system by using
Space.World
instead ofSpace.Self
.
The difference between position and local position:
Position:
- Position represents the position of the GameObject in the world space;
- It is relative to the global coordinate system of the scene;
- Changes to the position property affect the GameObject's position in entire scene.
Local Position:
- localPosition represents the position of the GameObject relative to its parent GameObject;
- It is relative to the coordinate system of the parent GameObject;
- Changes to the localPosition property affect the GameObject's position relative to its parent, not the entire scene.
Tudo estava claro?