Advertisement
norbyscook

v1 godot movement

Aug 19th, 2023
1,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # v1 good,
  2. # written for godot movement
  3. # it works well and is simple
  4. extends Node2D
  5.  
  6. # Declare member variables here.
  7. var velocity := Vector2.ZERO
  8. var direction := Vector2.ZERO
  9. var max_speed: float = 200
  10.  
  11. # Called every frame. 'delta' is the elapsed time since the previous frame.
  12. func _process(delta):
  13.    
  14.     direction = get_direction()
  15.    
  16.     if direction:
  17.         velocity = accelerate(0.1)
  18.     else:
  19.         velocity = decelerate(0.07)
  20.    
  21.     position += velocity * delta
  22.  
  23.  
  24. func decelerate(drag: float) -> Vector2:
  25.     return lerp(velocity, Vector2.ZERO, drag)
  26.  
  27. func accelerate(drag: float) -> Vector2:
  28.     var target_velocity: Vector2 = direction * max_speed
  29.     return lerp(velocity, target_velocity, drag)
  30.  
  31. func get_direction() -> Vector2:
  32.     var direction: Vector2 = Vector2.ZERO
  33.     direction.y = Input.get_axis("move_up", "move_down")
  34.     direction.x = Input.get_axis("move_left", "move_right")
  35.     return direction.normalized()
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement