Godot 2D Vector snippet
This commit is contained in:
parent
55f6772d03
commit
bf57f94044
1 changed files with 35 additions and 0 deletions
|
|
@ -0,0 +1,35 @@
|
|||
```python
|
||||
extends Sprite2D
|
||||
var direction := Vector2(0,0)
|
||||
var max_speed := 300.0
|
||||
var velocity = Vector2(0, 0)
|
||||
var boost_speed := 1500.0
|
||||
var normal_speed := 300.0
|
||||
var desired_velocity := Vector2()
|
||||
var steering_factor := 10
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
var direction := Vector2(0, 0)
|
||||
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()
|
||||
|
||||
if Input.is_action_just_pressed("boost"):
|
||||
max_speed = boost_speed
|
||||
get_node("Timer").start()
|
||||
|
||||
desired_velocity = max_speed * direction
|
||||
var steering = desired_velocity - velocity
|
||||
|
||||
velocity += steering * steering_factor * delta
|
||||
position += velocity * delta
|
||||
|
||||
if direction.length() > 0.0:
|
||||
rotation = velocity.angle()
|
||||
|
||||
|
||||
func _on_timer_timeout() -> void:
|
||||
max_speed = normal_speed
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue