Moving sprites

In the previous step, we added a script to a Sprite2D and used that script to set its position.

Here’s the script we have so far:

extends Sprite2D

func _ready():
    position = Vector2(100, 150)

Moving the sprite

Now we want to make the sprite move around the screen. Godot runs at 60 fps (frames per second) by default, and we’d like to the sprite to move a little bit each frame. Add this code to the script and run it to see what happens.

func _process(delta):
    position.x += 100 * delta

Did you see the sprite moving to the right? Let’s break down what’s happening.

Just as the _ready() function was called when the node was first started (by pressing “Play”), the _process() function is called every frame. This means any code you write in this function will be executed every 1/60 of a second. This is what the delta represents - the length of the frame in seconds.

What are we asking Godot to do every frame here? Take the sprite’s x coordinate and add 100 * 1/60 to it.

The result is that we have a sprite that’s moving 100 pixels per second to the right.

For more information about delta and why we use it in game development, see Understanding delta.

Using vectors

What if we want to move the sprite diagonally? That means we need to change the x and y coordinates at the same time. position is a vector - it contains both coordinates in one quantity. Vectors can be added, so let’s change the code to this:

func _process(delta):
    position += Vector2(100, 50) * delta

Run the code and you should see the sprite moving down and to the right.

Let’s make this a little easier to deal with by using variables:

extends Sprite2D

var velocity = Vector2(100, 50)

func _ready():
    position = Vector2(100, 150)

func _process(delta):
    position += velocity * delta

We’ve taken this quantity and made it a variable. You declare a variable by using the keyword var, giving the variable a name, and then assigning a value with =.

It’s often more convenient to declare variables at the top where they’re easy to find. We’ll also find it more convenient later, when we start making multiples of this sprite.

Randomizing the movement

Let’s mix this up and choose a random direction for the sprite to move in.

func _ready():
    position = Vector2(100, 150)
    velocity = Vector2.RIGHT.rotated(randf_range(0, TAU))
    velocity = velocity * randf_range(100, 400)

That’s a lot, so let’s break it down:

  1. First, we’re setting velocity to point to the right. Vector2.RIGHT is a built-in constant that represents the vector (1, 0).

  2. Next, we’re using Vector2’s rotated() method to rotate the vector. In the parentheses of rotated() it expects an angle.

  3. Since we want to rotate a random amount, we use randf_range() to get a random number between 0 and TAU.

    When working with angles, rather than _degrees_, GDScript (like most programming languages) uses _radians_. In radians, a full rotation is equal to `2 * PI` (or `TAU`) - equivalent to `360` degrees.
    
  4. Finally, we multiply velocity by another random number to give it a random speed.

Try running the scene a couple of times and you’ll see the sprite move in different directions.

func _process(delta):
    position += velocity * delta
    position.x = wrapf(position.x, -64, screensize.x+64)
    position.y = wrapf(position.y, -64, screensize.y+64)