Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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 acts like the GPS for a GameObject. It provides information about where the GameObject is located, how it is rotated, and its size in the game world. This component is crucial for positioning and orienting objects within your game.

Control the Transform Component with a 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's break it down:

  • Transform: This is the component that holds the position, rotation, and scale of the GameObject;
  • GameObject: The object in your scene that the script is controlling.

By using transform in your script, you can easily access and manipulate these properties to control how your GameObject behaves in the game world.

For example, using transform.position will give you the current position of the GameObject in the game world.

Whenever you see transform in a script, 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, allowing you to change their position, rotation, and scale directly through code.

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.

By understanding these components, you can effectively control the movement and orientation of objects in your Unity projects.

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 location 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 location in the entire scene.

Local Position:

  • localPosition represents the location 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 location 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