Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | indigo |
I’m using the following script for my enemy, a KinematicBody2D, to navigate to my player, another KinematicBody2D:
var destination = nav2d.get_closest_point(player.get_global_position())
var path_to_destination = nav2d.get_simple_path(get_global_position(), destination)
var starting_point = get_global_position()
var move_distance = SPEED * delta
for point in range(path_to_destination.size()):
var distance_to_next_point = starting_point.distance_to(path_to_destination[0])
if move_distance <= distance_to_next_point:
var move_rotation = get_angle_to(starting_point.linear_interpolate( path_to_destination[0], move_distance / distance_to_next_point))
var motion = Vector2(SPEED, 0).rotated(move_rotation)
move_and_slide(motion)
break
move_distance -= distance_to_next_point
starting_point = path_to_destination[0]
path_to_destination.remove(0)
The script seems to work, but only when the player is extremely close to the enemy. If the player is more than a couple of inches away, the enemy just stand still.
Any help?