Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- var s_max_speed = 4
- var s_force_magnitude = 0.05
- var s_turn_speed = deg2rad(3)
- var g_destination = null
- var g_velocity = Vector2(2, 0)
- var g_acceleration = Vector2.ZERO
- func _ready():
- pass
- func _physics_process(delta):
- g_velocity += g_acceleration
- g_velocity = _clamp_vector(g_velocity, s_max_speed)
- g_acceleration = Vector2.ZERO
- position += g_velocity
- _move_towards(g_destination)
- func _move_towards(location):
- # No destination assigned
- if g_destination == null:
- return
- # Get direction vector towards the target
- var desired_velocity = g_destination - global_position
- desired_velocity = desired_velocity.normalized()
- desired_velocity *= s_max_speed
- var steering_force = self._clamp_vector(desired_velocity, s_force_magnitude)
- g_velocity += steering_force
- var dir = g_velocity.angle_to(location)
- rotation = lerp_angle(rotation, dir, 0.1)
- func steer_towards(location):
- g_destination = location
- func _clamp_vector(vec, max_magnitude):
- var current_mag = vec.length()
- var multiplier = 1.0 \
- if current_mag <= max_magnitude else max_magnitude/current_mag
- return Vector2(vec.x * multiplier, vec.y * multiplier)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement