Advertisement
Guest User

Untitled

a guest
Nov 20th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1.  
  2. extends Node2D
  3.  
  4. var s_max_speed = 4
  5. var s_force_magnitude = 0.05
  6. var s_turn_speed = deg2rad(3)
  7.  
  8. var g_destination = null
  9. var g_velocity = Vector2(2, 0)
  10. var g_acceleration = Vector2.ZERO
  11.  
  12. func _ready():
  13. pass
  14.  
  15. func _physics_process(delta):
  16.  
  17. g_velocity += g_acceleration
  18. g_velocity = _clamp_vector(g_velocity, s_max_speed)
  19. g_acceleration = Vector2.ZERO
  20.  
  21. position += g_velocity
  22. _move_towards(g_destination)
  23.  
  24. func _move_towards(location):
  25.  
  26. # No destination assigned
  27. if g_destination == null:
  28. return
  29.  
  30. # Get direction vector towards the target
  31. var desired_velocity = g_destination - global_position
  32. desired_velocity = desired_velocity.normalized()
  33. desired_velocity *= s_max_speed
  34.  
  35. var steering_force = self._clamp_vector(desired_velocity, s_force_magnitude)
  36. g_velocity += steering_force
  37.  
  38. var dir = g_velocity.angle_to(location)
  39. rotation = lerp_angle(rotation, dir, 0.1)
  40.  
  41. func steer_towards(location):
  42. g_destination = location
  43.  
  44. func _clamp_vector(vec, max_magnitude):
  45. var current_mag = vec.length()
  46. var multiplier = 1.0 \
  47. if current_mag <= max_magnitude else max_magnitude/current_mag
  48.  
  49. return Vector2(vec.x * multiplier, vec.y * multiplier)
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement