DocumentatiiNotite/Miscari caracter

16 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-07-05 11:53:32 +03:00
Ziua 1:
Realizarea miscarii nodului: // circular
var speed = 400 # pixel per second
var angular_speed = PI # radian per second
func _process(delta):
rotation += angular_speed * delta
var velocity = Vector2.UP.rotated(rotation) * speed
position += velocity * delta
As we already saw, the var keyword defines a new variable. If you put it at the top of the script, it defines a property of the class. Inside a function, it defines a local variable: it only exists within the functions scope.
We define a local variable named velocity, a 2D vector representing both a direction and a speed. To make the node move forward, we start from the Vector2 classs constant Vector2.UP, a vector pointing up, and rotate it by calling the Vector2 method rotated(). This expression, Vector2.UP.rotated(rotation), is a vector pointing forward relative to our icon. Multiplied by our speed property, it gives us a velocity we can use to move the node forward.
We add velocity * delta to the nodes position to move it. The position itself is of type Vector2, a built-in type in Godot representing a 2D vector.