22 lines
557 B
Markdown
22 lines
557 B
Markdown
|
|
https://school.gdquest.com/courses/learn_2d_gamedev_godot_4/to_space_and_beyond/taking_control
|
||
|
|
|
||
|
|
```gdscript
|
||
|
|
extends Sprite2D
|
||
|
|
var direction := Vector2(0,0)
|
||
|
|
var max_speed := 600.0
|
||
|
|
var velocity = Vector2(0, 0)
|
||
|
|
|
||
|
|
func _process(delta: float) -> void:
|
||
|
|
|
||
|
|
direction.x = Input.get_axis("move_left", "move_right")
|
||
|
|
direction.y = Input.get_axis("move_up", "move_down")
|
||
|
|
|
||
|
|
if direction.length() > 1.0:
|
||
|
|
direction = direction.normalized()
|
||
|
|
|
||
|
|
velocity = direction * max_speed
|
||
|
|
position += velocity * delta
|
||
|
|
if direction.length() > 0.0:
|
||
|
|
rotation = velocity.angle()
|
||
|
|
```
|