So I am trying to get an an enemy to move along a path. It seems to go to the correct position however it doesn't smoothly go there. Once I hit the space button it instantly travels there. I am not sure how to do this. I originally thought that you should use move_and_slide for this but it has the same issue. I have tried linear_interpolation and lerp as well and nothing seems to be working correctly. Here is my move code.
func walk():
var tilemap = get_node("../Navigation2D/GroundTileMap")
rand.randomize()
var cells = tilemap.get_used_cells()
var randindex = rand.randi_range(0, cells.size())
var tile_center = tilemap.map_to_world(cells[150]) + tilemap.cell_size / 2
var points = get_node("../Navigation2D").get_simple_path(get_global_position(), tile_center, false)
var start_point = get_global_position()
if points.size() > 0:
for i in range(points.size()):
var distance = points[0] - start_point
if distance.length() > stop_distance and points.size() > 2:
position = lerp(start_point, points[0], 2)
else:
position = points[0]
break
start_point = points[0]
points.remove(0)
The cell portion or top part of the code is just getting a random tile in the scene to move to. I start off with gettting the start point which is just the global position. I then loop through the points along the path. I grab the distance and check to see if it is greater the stop distance which right now is 1.5. I also check the size of the points. then if those check out I start to lerp. But it just instantly moves to that position. At the end of the loop I set the start point to the starting point and remove the old start point from the array. I am not sure what Im doing wrong. This is all called when I hit the space bar like below.
func _process(delta):
if Input.is_action_just_released("ui_select"):
walk()
If someone could help out with this that would be awesome
EDIT : I updated the loop to a while loop since it makes more sense
while points.size() >= 1:
var distance = points[0] - start_point
if distance.length() > stop_distance and points.size() > 1:
# position = start_point.linear_interpolate(points[0], speed)
position = lerp(start_point, points[0], 2)
else:
position = points[0]
break
start_point = points[0]
points.remove(0)