Well, I tried normalizing the motion but it didn't do much. I eventually just settled on the script selecting a random point and using a directional variable in motion = direction.normalized().
Here's the parts of the script that I've changed:
var destination = Vector2()
func _ready():
_timer = Timer.new()
add_child(_timer)
_timer.connect("timeout", self, "_on_Timer_timeout")
_timer.set_wait_time(randi() % 6+6)
_timer.set_one_shot(false) # Make sure it loops
_timer.start()
randomize()
destination = Vector2(self.get_global_position().x+100, self.get_global_position().y+100)
pass
func _physics_process(delta):
var direction = destination - self.position
if speed <= MAX_SPEED:
speed = speed + ACCELERATION
motion = direction.normalized() * speed
else:
motion = direction.normalized() * MAX_SPEED
if direction.x > 0:
$AnimatedSprite.flip_h = true
else:
$AnimatedSprite.flip_h = false
if speed < 10:
$AnimatedSprite.speed_scale = .1
else:
$AnimatedSprite.speed_scale = 1
move_and_slide(motion)
pass
func _on_Timer_timeout():
randomize()
_timer.set_wait_time(randi() % 6+6)
_timer.set_one_shot(false) # Make sure it loops
_timer.start()
destination = Vector2(self.get_global_position().x + randi() % 600-300, self.get_global_position().y + randi() % 600-300)
speed = 0
pass
But now what I'm trying to do is give my AI an Area2D that acts as the destination, and when the AI enters that area it sends a message to the AI's script to slow down (speed = speed - ACCELERATION). However, I'm having trouble sending a signal from the Area2D to the AI that it has entered that area.