Menu
CoddyTech

Move Diagonally

Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 10 of 28.

To move on both axes, you can write one line for x and one for y:

position.x += 100 * delta
position.y += 50 * delta

A speed with a direction is a velocity. One Vector2 can hold both speeds: Vector2(100, 50) is 100 pixels a second right and 50 down. Multiplying it by delta multiplies both numbers:

position += Vector2(100, 50) * delta

Both forms do the same thing. A negative number points the other way: Vector2(-100, -50) goes left and up.

challenge icon

Challenge

Easy

Make the robot move right and down at once.

  1. In _process(), replace the line with one that adds a Vector2 to position: 120 pixels a second right and 45 down.
  2. Multiply the Vector2 by delta.

Press Play: after two seconds the robot is on the floor, at (560, 270). Then Check.

Try it yourself

extends Node2D

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

All lessons in Nodes and Scenes

Practice on your own: Godot playground