Turrets intelligent aiming

I added extra code to make the turrets aim ahead of the enemy, based upon where it’s soon going to be and the distance between the turret and the enemy, it works great.

var enemy_path:Path3D:
	set(value):
		enemy_path = value
		if(aim_path_follow_point==null):
			aim_path_follow_point = PathFollow3D.new()
			enemy_path.add_child(aim_path_follow_point)
			aim_path_follow_point.loop = false
var aim_path_follow_point:PathFollow3D

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass

func _physics_process(_delta: float) -> void:
	var enemies = enemy_path.get_children().filter(func(e): return e is Enemy)
	var enemy = enemies.back()
	if(enemy == null):
		return
	var aim_point = enemy.global_position
	
	if(aim_path_follow_point != null):
		var distance = (global_position - aim_path_follow_point.global_position).length()
		aim_path_follow_point.progress = enemy.progress + 0.035 * enemy.speed * distance
		aim_point = aim_path_follow_point.global_position

	look_at(aim_point,Vector3.UP,true)

We add an additional PathFollow3D to the path per turret, we now have to filter the enemypath’s children to ensure we’re only picking enemies in our aiming code. Move the aim PathFollow3D to be slightly ahead in progress than the enemy, based on the enemy’s speed and the distance between the turret and the enemy (it’ll take longer for the projectiles to reach far away enemies and fast enemies will move further along the path in the same amount of time)

1 Like

I was wondering if anybody would implement this tactic at some point - nicely done!

1 Like

Privacy & Terms