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 * deltaA 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) * deltaBoth forms do the same thing. A negative number points the other way: Vector2(-100, -50) goes left and up.
Challenge
EasyMake the robot move right and down at once.
- In
_process(), replace the line with one that adds aVector2toposition:120pixels a second right and45down. - Multiply the
Vector2bydelta.
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
1Your First Godot Game
What Is Godot?Ready Runs OncePrint in GodotThe Scene TreeSet a PositionMove a Node Once2Every Frame
Process Runs Every FrameDeltaPixels per SecondMove DiagonallySpin With RotationGrow With ScalePractice on your own: Godot playground