Learn

Chapter 04 Variables & Data Types

#033 Vectors

Where is the enemy? Which way is the player? How far away? All three answers are Vectors.

  • 1:12 video
  • Written lesson
  • Beginner
Video lesson: Vectors, 1:12

Three numbers together

Locations, directions, velocities: each one needs three numbers together.

A Vector holds three floats: X, Y and Z. Make Vector builds one from three numbers. In Unreal, X points forward, Y points right, and Z points upward. One unit is one centimeter.

Get Actor Location returns one. Split the pin, or use Break Vector, to read a single axis. Subtract two locations, and you get the vector from one to the other.

Example: an enemy chasing the player

Player Location minus Enemy Location points toward the player. Vector Length gives the distance: 500 centimeters, so five meters.

Normalize keeps the direction, but sets the length to one. Multiply by Speed, and the enemy moves steadily, near or far.

Common mistakes

  • Skipping Normalize. The raw difference is as long as the distance, so far enemies race in and near ones crawl.
  • Wrong order. Enemy minus Player points away from your target.

Need only the distance? Get Distance To takes two actors and does the math for you.

In short

Three numbers: where, which way, and how far.

Covered in this lesson

  • Vector
  • Make Vector
  • Break Vector
  • Get Actor Location
  • Vector Length
  • Normalize
  • Get Distance To

Newsletter

Development updates are coming to your inbox.

The newsletter opens soon. Until then, new lessons, tools and Beach Hunters progress appear first on our channels, linked below.