Course contentsShow
← Unity Game Development
Lesson 94 of 8282. Scripting and LifecycleFree lesson

Manipulating Transform Position and Rotation

Learners will programmatically modify the position and rotation properties of a Transform component to control the placement of game objects.

In this lesson, you will learn how to take direct control of your game objects by programmatically modifying their position and rotation using the Transform component.

Every GameObject has a Transform component attached to it by default. This component acts as the "anchor" for that object in your 3D or 2D world. While you can move objects in the Scene view, writing code to change these values allows your game to react to player input or environmental triggers.

Moving an Object

To change where an object is located, you modify its transform.position property. This property accepts a Vector3, which is a mathematical structure representing X, Y, and Z coordinates.

If you want to nudge an object forward, you can update its position inside your Update method:

void Update()
{
    // Moves the object along the X-axis by 0.01 units every frame
    transform.position += new Vector3(0.01f, 0, 0);
}

Rotating an Object

Rotation is handled through transform.rotation or transform.eulerAngles. While rotation uses complex mathematics called Quaternions, eulerAngles allows you to set rotation using familiar degrees (like 0 to 360).

If you want to spin an object, you can add to its existing rotation:

void Update()
{
    // Rotates the object by 1 degree around the Y-axis every frame
    transform.eulerAngles += new Vector3(0, 1f, 0);
}

Local vs. Global Space

Remember your previous lesson on coordinate systems: transform.position and transform.eulerAngles represent the position and rotation in global (World) space. If you want to move or rotate an object relative to its own orientation (for example, moving "forward" regardless of which way the object is facing), you would use methods like transform.Translate or transform.Rotate, which are built-in tools for applying these changes relative to the object's local axes.

By mastering these properties, you gain the power to make your objects dynamic, allowing them to chase players, rotate items, or follow scripted paths through your game world.

Key Takeaway: Programmatically modifying transform.position and transform.eulerAngles allows you to precisely control the placement and orientation of any GameObject in your scene.