Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Transform Component | Write your First Script
Unity for Beginners
course content

Course Content

Unity for Beginners

Unity for Beginners

1. Unity Introduction
2. Write your First Script
3. Unity Physics
4. Unity UI and Sounds
5. Polishing and Export your Game

book
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 of Space.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.
1. What is the primary purpose of the position property in the Transform component?
2. How is localPosition different from position in Unity's Transform component?
What is the primary purpose of the position property in the Transform component?

What is the primary purpose of the position property in the Transform component?

Select the correct answer

How is localPosition different from position in Unity's Transform component?

How is localPosition different from position in Unity's Transform component?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt