Godot 4.2
I have a spherical flying enemy using a state machine with a couple of states. When the enemy gets near the player they transition to the chase state where they get the direction to the player before flying towards them.
Every physics frame the state machine calls the physics_process function in the current state.
Chase Script:
func physics_process( delta:float ):
if los_shapecast.is_colliding():
for i in los_shapecast.get_collision_count():
var collision = los_shapecast.get_collider(i)
if collision is Player and state_machine.get_timer("Shoot_Cooldown").is_stopped():
state_machine.transition_to("Shoot")
return
los_shapecast.position = entity.position
los_shapecast.look_at(player.global_position)
var dir:Vector2 = entity.global_position.direction_to(player.global_position)
current_speed += (dir * acceleration).rotated(randf_range(-random_spread, random_spread))
current_speed = current_speed.limit_length(chase_speed)
entity.velocity = current_speed
for i in entity.get_slide_collision_count():
var collision = entity.get_slide_collision(i)
if collision.get_collider() is MoveableObject:
collision.get_collider().call_deferred("apply_central_impulse", -collision.get_normal() * entity.motion.length() )
entity.move_and_slide()
Once the enemy is close enough to the player it prepares to exit the state by calling this function exit()
Chase Script:
func exit():
nav_timer.disconnect("timeout", Callable(self, "_on_nav_timer_timeout"))
entity.velocity = entity.velocity.limit_length(chase_speed)
current_speed = Vector2.ZERO
and enters the “Shoot” state where it plays an animation, creates a projectile at the end, then transitions back to Chase.
Shoot Script:
func _on_shoot_anim_over(anim_name):
var fireball_instance = projectile.instantiate()
GlobalScript.add_child(fireball_instance)
var points:Vector2 = entity.global_position + PROJECTILE_SPAWN_LOCATION
var push:Vector2 = fireball_instance.object_push
if !entity.sprite.flip_h:
points.x = abs(points.x)
push.x = abs(push.x)
var hitbox_info = {
"global_position": points,
"projectile_owner": entity
}
fireball_instance.set_parameters(hitbox_info)
fireball_instance.set_future_collision()
fireball_instance.set_past_collision()
state_machine.transition_to("Chase")
then I call the exit function for shoot:
Shoot Script:
func exit() -> void:
entity.anim_player.disconnect("animation_finished", Callable(self, "_on_shoot_anim_over"))
shoot_cooldown.start()
and then the enter function for chase again:
Chase Script:
func enter(_msg:= {}):
nav_timer.connect("timeout", Callable(self, "_on_nav_timer_timeout"))
nav_timer.start()
current_speed = Vector2.ZERO
entity.velocity = entity.velocity.limit_length(chase_speed)
At this point for 1 physics frame, the enemy’s velocity spikes up to numbers of 25,000 when the chase_speed variable is currently set in the editor as 425,
How can I restrict the enemy’s velocity so that the length doesn’t exceed the variable chase_speed, since current_speed.limit_length(chase_speed) isn’t working?