Godot Version
4.2.1
Question
I have an enemy and I want it to wander around randomly. I’ve already achieved this to some degree however i’d like for the enemy to be more likely to wander in certain direction at certain times.
basically whenever the enemy is far away from the “wander_center” the enemy should be more likely to wander towards the center and vice versa. When the enemy is close to the center it should be more likely to move away from the center.
I’m not sure how to get started on this
Enemy movement code:
extends State
const SPEED = 100.0
const WANDER_RADIUS = 100
@onready var animation_tree = $"../../AnimationTree"
@onready var timer = $movement_timer
@export var idle_state : State
@export var dead_state : State
@export var hurt_state : State
var target_position: Vector2 = Vector2.ZERO
func on_enter(): # is triggered everytime the enemy enters this state
timer.start(randf_range(1,2.5))
target_position = Vector2(randf_range(-WANDER_RADIUS,WANDER_RADIUS),randf_range(-WANDER_RADIUS,WANDER_RADIUS))
func on_exit(): #triggered just before exiting this state
pass
func state_process(_delta): #returns a vector from global_position to target_point
var direction = character.global_position.direction_to(target_position)
if character.global_position.distance_to(target_position) >= 30:
character.velocity = character.global_position.direction_to(target_position) * SPEED
else: # if not moving and timer has not stopped - transition early
timer.stop()
next_state = idle_state
print("has arrived")
if direction != Vector2.ZERO: #handles latest direction
character.latest_direction = direction
if character.health <= 0: #transition to dead state
character.dead = true
next_state = dead_state
if character.hurt: # transition to hurt state
next_state = hurt_state
character.move_and_slide()
func _on_movement_timer_timeout(): #transition to idle state
next_state = idle_state