I’m confused, it should work. Can you tell me how to fix it, or show me a correct example?
if SD:
var to_two = false
if global_position.distance_to(pos11) <= 0.002 and !to_two:
set_target_position(pos22)
to_two = true
if global_position.distance_to(pos22) <= 0.002 and to_two:
set_target_position(pos11)
to_two = false
It should go to point 1 and then to point 2. But it only goes to point 1 and keeps going. But it only goes to point 1 because of the direct call from the function (code number 2), but it should move because of the first piece of code.
I assume you’re trying to make something move back and forth between two points. Your to_two var is going to start false every time though that code, and your distance check is only going to work if you wind up almost exactly on the position; if you pass through it at speed, you may not be “on” the point when you’re checking distance.
I’d suggest something like:
# Untested...
var patrol_waypoints: Array = []
var patrol_pingpong: bool
var patrol_loop_forward: bool # true -> forward, false -> reverse
var patrol_index: int
var patrol_unit: CharacterBody3D
var patrol_current_target: Vector3
var patrol_waypoint_range: float
func setup_patrol(unit: CharacterBody3D, pingpong: bool, points: Array):
patrol_waypoints = points
patrol_pingpong = pingpong
patrol_loop_forward = true
patrol_index = 0
patrol_unit = unit
patrol_current_target = patrol_waypoints[0]
patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)
func update_patrol(delta: float):
var cur_dist = patrol_unit.global_position.distance_to(patrol_current_waypoint)
# If we're farther than we were, we've reached it and overshot slightly, so we
# target the next waypoint.
if cur_dist > patrol_waypoint_range || cur_dist < 0.002:
if patrol_loop_forward: # Traverse forwards.
if patrol_index < (patrol_waypoints.size() - 1): # Mid-loop, go forwards.
patrol_index += 1
elif patrol_pingpong: # End & pingpong, reverse.
patrol_index -= 1
patrol_loop_forward = false
else: # End & not pingpong, wrap.
patrol_index = 0
else: # Traverse backwards.
if patrol_index > 0: # Mid loop, go backwards.
patrol_index -= 1
elif patrol_pingpong: # End & pingpong, go forwards.
patrol_loop_forward = true
patrol_index += 1
else: # End & not pingpong, wrap.
patrol_index = patrol_waypoints.size() - 1
# Set up the new point.
patrol_current_target = patrol_waypoints[patrol_index]
patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)
func _process(delta: float):
[...stuff...]
# Do patrol things.
if is_instance_valid(patrol_unit):
update_patrol(delta)
That may be more complex than what you want; it supports an arbitrary number of patrol points, and either cycling through the points or pingponging back and forth from one end to the other.
For distance checks, we keep the 0.002 range check, but we add another check to see if we’ve got further away; we treat that as an overshoot and move to the next waypoint.
Here’s a simpler version that doesn’t do pingpong or reverse loops:
var patrol_waypoints: Array = []
var patrol_index: int
var patrol_unit: CharacterBody3D
var patrol_current_target: Vector3
var patrol_waypoint_range: float
func setup_patrol(unit: CharacterBody3D, points: Array):
patrol_waypoints = points
patrol_index = 0
patrol_unit = unit
patrol_current_target = patrol_waypoints[0]
patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)
func update_patrol(delta: float):
var cur_dist = patrol_unit.global_position.distance_to(patrol_current_waypoint)
# If we're farther than we were, we've reached it and overshot slightly, so we
# target the next waypoint.
if cur_dist > patrol_waypoint_range || cur_dist < 0.002:
if patrol_index < (patrol_waypoints.size() - 1): # Mid-loop, go forwards.
patrol_index += 1
else: # End. Wrap.
patrol_index = 0
# Set up the new point.
patrol_current_target = patrol_waypoints[patrol_index]
patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)
func _process(delta: float):
[...stuff...]
# Do patrol things.
if is_instance_valid(patrol_unit):
update_patrol(delta)