Course contentsShow
← Unity Game Development
Lesson 43 of 8281. Unity FoundationsFree lesson

Applying Vector Addition for Positional Offsets

Students will manipulate object positions by adding vectors to represent displacement and relative movement in 3D space.

In this lesson, you will learn how to shift an object’s location in 3D space by adding a "displacement vector" to its current position.

Understanding Positional Offsets

In Unity, the transform.position property stores an object's location as a Vector3 (x, y, z). Sometimes, you don't want to move an object to an absolute coordinate; instead, you want to nudge it a specific distance from where it already sits. This is where Vector Addition comes in.

Think of it like giving directions: if you are at a specific street corner, and you walk "2 blocks North and 1 block East," you are adding a displacement vector (1, 0, 2) to your current position.

Applying Addition in Code

When you add two vectors together, Unity adds their individual components (X to X, Y to Y, and Z to Z). To move an object, you take its current position and add a new vector that represents the desired shift.

Here is how you can use this in a script:

// Define how much we want to move the object
Vector3 offset = new Vector3(0, 5, 0); 

// Add the offset to the current position and apply it back
transform.position = transform.position + offset;

In this example, if your object was at (1, 1, 1), adding the offset of (0, 5, 0) results in a new position of (1, 6, 1). The object has "slid" 5 units upward along the Y-axis.

Why This Matters

Using vector addition is the foundation for procedural movement. Instead of guessing where an object should end up, you define a relative change. This is incredibly useful for features like:

  • Knockback effects: Pushing an object away from a collision point.
  • Floating items: Bobbing an object up and down by adding a small, repeating offset.
  • Step-based movement: Moving a character a fixed distance each time a key is pressed.

By mastering this, you stop thinking about "where an object is" and start thinking about "how an object changes" relative to its current environment.

Key Takeaway: Vector addition allows you to shift an object by adding a displacement vector to its current position, enabling precise, relative movement in 3D space.