#Making the Wander state in Enemy AI wait?

5 messages · Page 1 of 1 (latest)

timid sierra
#

I'm trying to get my enemy "wander" state to wait a random amount before going back to moving?
I'm going crazy because this code semi-worked until I started trying to use timers to make the enemy "wait" before deciding a new target location:

            if direction == Vector2():
                direction = global_position.direction_to(target_position)
            if global_position.distance_to(target_position) <= 12: # If withing 12 units of the destination, wait and then get a new destination
                direction = Vector2(0,0)
                await get_tree().create_timer(1).timeout
                target_position == get_wander_location()
                direction = global_position.direction_to(target_position)
            await get_tree().create_timer(1).timeout
            target_position == get_wander_location()
            if velocity == Vector2(0,0) and direction == Vector2(0,0): # because these were printing 0,0 randomly
                target_position == get_wander_location()
                direction = global_position.direction_to(target_position)```

Additional explanation: get_wander_location() returns a point within 32 units of the spawn location (simply the global_location when the node is ready)
dusk crater
#

It should be implemented as 2 states.
Wander and Idle

#

Wander choost a point or direction at beginning of state then walk. After arriving or after certain amount of time passed, you go to idle state that enemy will stand still for some amount of time
Then you go back to wander again

timid sierra
# dusk crater Wander choost a point or direction at beginning of state then walk. After arrivi...

I've worked a bit more on it but the result is weird.
Is this not right?

            # Stop for a few seconds, then find a new state
            direction = Vector2(0,0)
            velocity = Vector2(0,0)
            target_position = get_wander_location()
            await get_tree().create_timer(randf_range(1, 3)).timeout
            state = STATE.WANDER
        STATE.WANDER: # Walk around a radius from spawn location
            if target_position == Vector2(0,0):
                target_position == get_wander_location()
            if global_position.distance_to(target_position) <= 4:
                direction = Vector2(0,0)
                velocity = Vector2(0,0)
                await get_tree().create_timer(randf_range(1, 3)).timeout
                state = STATE.IDLE
            direction = global_position.direction_to(target_position)```
dusk crater
#

I'm not familiar with await syntax. Is that Godot 4 ?
anyway, I suggest you to implement calling a specific function on entering and exiting state. That is very helpful to have.
For example, when you start wander, on_entering you can do randomized timer time, start the timer, and getting new position or direction to walk to. And then when timer ends or when you reach destination, you can change to idle state. On entering idle state, you can set velocity to 0, again randomized timer time and start the timer. When timer ends you then can change state back to wander