0
0
get_node("Timer").stop()
if Input.is_action_just_pressed("boost"): direction = Vector2.ZERO
_process(0.5 * delta)
This code extends Sprite and defines three variables: boost_speed, normal_speed, and max_speed. max_speed is the speed that the player will move at.Normal speed is set to 600 and boost_speed is set to 1500.Velocity is set to Vector2.ZERO, which means that the player is at (0,0). Drag_factor is set to 0.1 to give the player a bit of drag. _process() is called whenever the player moves their left or right stick. If the player is moving up or down, _process() is called with a different delta value. If the player presses the "boost" key, _process() sets max_speed to the boost_speed and gets the node "Timer" started. process() then sets desired_velocity to the direction * max_speed and calculates the steering_vector by subtracting velocity from desired_velocity - velocity. Velocity is then increased by the steering_vector * drag
extends Sprite
var boost_speed := 1500.0
var normal_speed := 600.0
var max_speed := normal_speed
var velocity := Vector2.ZERO # Velocity = Van Toc, ZERO = (0,0)
var drag_factor := 0.1
func _process(delta: float) -> void:
var direction := Vector2.ZERO
direction.x = Input.get_axis("move_left", "move_right")
direction.y = Input.get_axis("move_up", "move_down")
if direction.length()>1.0: # de tau khi bay cheo' van toc ko tang len
direction = direction.normalized()
if Input.is_action_just_pressed("boost"): #boost key: "space"
max_speed = boost_speed
get_node("Timer").start()
# Node "timer" da duoc thiet lap cu sau 0.5s se thuc hien func o line 29
var desired_velocity = direction * max_speed
var steering_vector = desired_velocity - velocity
velocity += steering_vector * drag_factor
position += velocity * delta
rotation = velocity.angle()
func _on_Timer_timeout() -> void:
max_speed = normal_speed