Enemy Randomly goes up into the air and continues forever?

Godot Version

Godot 4.2.Stable

Question

I have no idea what causes it but sometimes the enemy jsut decides to get flung upwards and just continues going up and it seems to happen at random.

Here is my enemy code:

extends CharacterBody3D

@onready var player = $"../CharacterBody3D"
@onready var navAgent: NavigationAgent3D = get_node("NavigationAgent3D")

@export var SPEED = 10.0
@export var Health = 3
var ded = false
var target_velocity = Vector3.ZERO
var fall_acceleration = 75
var can_detect = false
var current_location
var next_location
var new_velocity
var ammo = preload("res://Weapons/RigidBodys/Pistol/Pistol.tscn")
var ammo2 = preload("res://Weapons/RigidBodys/Smg/Smg.tscn")
var heart = preload("res://Items/Hearts/heart.tscn")
func _physics_process(delta):
	if can_detect == true:
		var LOOK_AT_ME2 = Vector3(player.position.x,position.y,player.position.z)
		look_at(LOOK_AT_ME2, Vector3.UP)
	
	if Health <= 0:
		if $".." != null:
			var random = randi_range(0,5)
			$"..".max_amt_of_eneimes -= 1
			if random == 4:
				var ammoinstantce = ammo.instantiate()
				get_parent().add_child(ammoinstantce)
				ammoinstantce.name = "Pistol"
				ammoinstantce.position = position
			elif random == 5:
				var ammo2instantce = ammo2.instantiate()
				get_parent().add_child(ammo2instantce)
				ammo2instantce.name = "Smg"
				ammo2instantce.position = position
		ded = true
		var random_heart = randi_range(1,3)
		if random_heart == 3:
			var mememeemme = heart.instantiate()
			get_parent().add_child(mememeemme)
			mememeemme.position = position
		
		queue_free()
	if can_detect == true:
		current_location = global_transform.origin
		next_location = navAgent.get_next_path_position()
		new_velocity =(next_location - current_location).normalized() * SPEED
	
	
	if can_detect == true:
		velocity = new_velocity
	
	if can_detect == true:
		move_and_slide()

func Damaged():
	can_detect = true
	Health -= player.Damage

func updateTargetLocation(target_location):
	if can_detect == true:
		navAgent.set_target_position(target_location)





func _on_enemy_hit_body_entered(body):
	if body.is_in_group("Characters"):
		body.Damaged()
		await get_tree().create_timer(1.0).timeout 


func _on_area_3d_body_entered(body):
	if body.is_in_group("Characters"):
		can_detect = true


func _on_player_inside_body_entered(body):
	if body.is_in_group("Characters"):
		await get_tree().create_timer(1).timeout
		$AnimationPlayer.play("Attaclk")
		$Hand/Enemy_Hit/CollisionShape3D.disabled = false
		await get_tree().create_timer(0.8).timeout
		$Hand/Enemy_Hit/CollisionShape3D.disabled = true
		$AnimationPlayer.play_backwards("Attaclk")


Here is how i am spawning them:

func _on_die_timeout():
	if $DunMesh.done:
		random_enemies = test
		if max_amt_of_eneimes <= 300:
			if random_enemies <= 4: 
				enemy2 = enemy.instantiate().duplicate()
			if random_enemies >= 3:
				weapon_enemy2 = weapon_enemy.instantiate().duplicate()
				
			if enemy2 != null || weapon_enemy2 != null && amt_of_enimie_spawns != 1 && get_node("Enemt_Spawn_" + str(amt_of_enimie_spawns)) != null:
				if random_enemies <= 4: 
					enemy2.name = "Close_Enemy"
					add_child(enemy2)
					max_amt_of_eneimes += 1
					enemy2.position = get_node("Enemt_Spawn_" + str(amt_of_enimie_spawns)).position + Vector3(0.1,.3,0)
					test = randi_range(1,6)
				if random_enemies >= 3 && weapon_enemy2 != null:
					weapon_enemy2.name = "Weapon_Enemy"
					add_child(weapon_enemy2)
					max_amt_of_eneimes += 1
					weapon_enemy2.position = get_node("Enemt_Spawn_" + str(amt_of_enimie_spawns)).position + Vector3(0.1,.3,0)
					test = randi_range(1,6)
				amt_of_enimie_spawns = rng.randi_range(2,t)

@ChickenTendie007 It really could be a number of things. Try printing out the new_velocity to see what that is being set to. It could be adding velocity in the Z direction.

Check out the NavigationAgents movement example for how they recommend setting the velocity in this situation, if you haven’t already, Using NavigationAgents — Godot Engine (stable) documentation in English

Also, your LOOK_AT_ME2 variable is potentially missing the player in the Y position?

Thanks after looking at the NaviagationAgents I seemed to have set mine up extremely poorly and changed the way the enemy moves around and that seemed to fix it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.