Godot version 3.5.1 stable
Something really weird is happening in my code and I'm not sure why.
When I print:
print(nav_agent.is_target_reached())
it prints "true".
When I print:
print(nav_agent.is_target_reachable())
it prints "true".
So I want to move my agent when the target is reachable and the target is not reached, so I used this statement (notice the "!" in front of the is_target_reached):
if (!nav_agent.is_target_reached() and nav_agent.is_target_reachable()):
It evaluates to true and still runs the logic inside the if statement which doesn't make sense to me, since both are true my NOT operator should make it so the if statement evaluates (false and true) and skip to the else statement.
But when I evaluate this expression (just swapped the sides of the conditions):
if (nav_agent.is_target_reachable() and !nav_agent.is_target_reached()):
The result is false as it should be.
I tried putting the !nav_agent.is_target_reached() in parenthesis but it still evaluates as true.
For more context:
func _process(delta):
# Nav agent movement
nav_agent.set_target_location(destination)
if (nav_agent.is_target_reachable() and !nav_agent.is_target_reached()):
var next_loc = nav_agent.get_next_location()
var direction = self.translation.direction_to(next_loc)
velocity = direction * move_speed
else:
velocity = Vector3.ZERO
# apply gravity
velocity.y -= gravity * delta
# apply all movement
self.move_and_slide(velocity, Vector3.UP)
The weird thing is, when I do the same inside of print statements, it always outputs correctly:
print(nav_agent.is_target_reached() and nav_agent.is_target_reachable()) # True
print(!nav_agent.is_target_reached() and nav_agent.is_target_reachable()) # False
print(nav_agent.is_target_reachable() and nav_agent.is_target_reached()) # True
print(nav_agent.is_target_reachable() and !nav_agent.is_target_reached()) # False
Does anyone know why this would happen?
Also, does anyone know why my character is slowly sinking in the ground? I read somewhere that we should always apply gravity otherwise we won't get the is_on_floor flag from the kinematicBody but I'm still not getting the flag... Any idea what could be causing this as well? Maybe its related to my current issue.