Menu

Godot Vector2: Positions, Directions and Vector Math

Vector2 is how Godot stores 2D positions, directions and velocities. Create vectors, add and scale them, get length and distance, normalize a direction, use angles, and move toward a target.

This page includes runnable editors. Edit, run, and see output instantly.

A Vector2 holds two numbers, x and y. Godot uses it for every position, direction and velocity in 2D:

(100.0, 50.0)
100.0 50.0
(120.0, 60.0)

The values are floats, even when you write whole numbers. In Godot's 2D space, x grows to the right and y grows down the screen.

Built-in Directions

(0.0, 0.0) (1.0, 1.0)
(0.0, -1.0) (0.0, 1.0)
(-1.0, 0.0) (1.0, 0.0)

Because y points down, Vector2.UP is (0, -1). A jump therefore sets velocity.y to a negative number.

Vector Math

Adding vectors moves a point. Multiplying by a number scales a vector:

(4.0, 6.0)
(2.0, 2.0)
(6.0, 8.0)
(1.5, 2.0)
(3.0, 8.0)

a - b is the vector that goes from b to a. That one fact powers most game math: aiming, chasing and measuring distance.

Length, Distance and Direction

50.0
50.0
(0.6, 0.8)
(0.6, 0.8)
  • length() is the size of a vector. distance_to() is the length of the vector between two points.
  • normalized() keeps the direction and sets the length to 1. direction_to() is (target - self).normalized() in one call.

Multiply a direction of length 1 by a speed to get a velocity. This is how an enemy chases the player:

extends CharacterBody2D

@export var speed := 120.0
@onready var player: Node2D = %Player

func _physics_process(_delta):
	velocity = global_position.direction_to(player.global_position) * speed
	move_and_slide()

Normalizing Input

Holding right and down at once gives the input vector (1, 1), whose length is about 1.41. Without normalizing, diagonal movement is 41% faster:

1.41
1.00

Input.get_vector("left", "right", "up", "down") reads four input actions and already returns a vector no longer than 1, so it is the usual way to read movement keys.

Angles and Rotation

Angles in Godot are in radians. angle() gives a vector's angle from the positive x axis, and rotated() turns a vector:

45.0 degrees
(0.0, 1.0)
(-1.0, 0.0)

Turning RIGHT by 90 degrees points it down here, again because y grows downward. To point a node at a target, use look_at(target_position), or set rotation = direction.angle().

Moving Toward a Target

(3.0, 0.0)
(5.0, 0.0)
(3.0, 4.0)
  • move_toward(to, delta) moves a fixed amount and never overshoots. Use speed * delta as the amount for frame-rate independent movement.
  • lerp(to, weight) goes a fraction of the way.
  • limit_length(max) caps a velocity at a top speed.

Comparing Vectors

Vector2 stores 32-bit floats, so math results are rarely exact. Compare with is_equal_approx():

(-0.0, 1.0)
false
true

Vector2i for Grids

Vector2i holds integers. Tile maps and grid games use it for cell coordinates:

(4, 4)
(3, 4)
(96.0, 128.0)

For 3D the same methods exist on Vector3, with a z component.

Frequently Asked Questions

What is a Vector2 in Godot?

A Vector2 is a pair of floats, x and y. Godot uses it for 2D positions (position), directions, velocities (velocity), sizes and scale. In 2D, positive x points right and positive y points down.

What does normalized() do in Godot?

It returns a vector with the same direction and a length of 1. Multiply it by a speed to move in that direction at that speed: velocity = direction.normalized() * 300. Normalizing input also stops diagonal movement from being faster.

How do I get the distance between two nodes in Godot?

a.global_position.distance_to(b.global_position) gives the distance in pixels. For comparisons only, distance_squared_to() is a little faster, because it skips the square root.

Why is Vector2.UP (0, -1) in Godot?

Godot's 2D y axis points down the screen, like most 2D image and UI systems. So up is negative y, and a jump sets a negative velocity.y.

What is the difference between Vector2 and Vector2i?

Vector2 holds floats and is used for positions and movement. Vector2i holds integers and is used for grid coordinates such as tile map cells. Convert with Vector2i(v) (which drops the decimals) and Vector2(vi).

Coddy programming languages illustration

Learn to code with Coddy

Get started