Vectors are an important concept in game development
They are used to represent a position or direction in code.
In 2D a vector would represent the X and Y axes
While in 3D, it would represent the X, Y and Z axes.
2D Vectors represent the X and Y axis values of something. An example of this is a node’s position property,
which can be modified in the Inspector with any 2D node selected.
Set texture as Player.png
Add a Script to the Sprite
Moving the player with code
To modify a node’s position in code,
Use the global_position variable
to get and set the node’s position.
Now access the X and Y values individually
by placing a dot in between the name and the property.
We can try this by setting the x position to 200 in the _ready function:
Running the scene now will start the game with the Player Sprite 200 pixels to the right.
We can also create our own vectors instead
of setting the position vector individually.
We can create our own vector variable
and then assign this vector to the node’s
position to move our Player node to
Player has moved diagonally down and to the right
This knowledge can also be transferred onto 3D, with the only difference being the additional Z axis.
You also need to use Vector3 instead of Vector2 when declaring a 3D vector, so that you can use the
additional Z value when creating it.
Moving the Player along a Vector
The _process function is a loop
any code inside it runs continuously.
If we multiply our Vector by 30
the Player moves at 30 pixels PER FRAME
To make our Player move smoothly