In Godot Engine, Vector2 is the most essential data type for 2D game development. It is a built-in engine type consisting of two floating-point components, x and y, used to represent positions, directions, velocities, scales, and any other pair of numerical values in a 2D space.

Whether you are calculating the trajectory of a projectile or moving a character across the screen, understanding the math and logic behind Vector2 is fundamental to creating a polished gaming experience.

Understanding the Godot 2D Coordinate System

Before diving into code, it is crucial to recognize how Godot handles 2D space. Unlike standard Cartesian coordinates taught in many mathematics courses, Godot follows a "Y-Down" orientation.

  • X-axis: Increases to the right.
  • Y-axis: Increases downward.
  • Origin (0,0): Typically represents the top-left corner of the viewport.

In our practical implementation of top-down shooters or platformers, failing to account for the downward-pointing Y-axis is the most common cause of "inverted" movement bugs. If you want an object to move "up" the screen, you must subtract from its y value or use the negative Vector2.UP constant.

Creating and Accessing Vector2 in GDScript

In GDScript, creating a vector is straightforward. You can initialize it with specific values or use built-in constants.

Initialization Syntax