Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | potatobanana |
my tower just atck the first enemy enter area2d, and ignore all enemy that already inside aread 2d.
1how should i make my tower attack the rest enemy in area?
2 how can i make my tower attck enemy that close to end goal, or atck enemy that have low health first?
func target_attk(delta):
if target:
var target_dir = (target.global_position - global_position).normalized()
var my_current_dir = Vector2.RIGHT.rotated($turrent.global_rotation)
$turrent.global_rotation = my_current_dir.linear_interpolate(target_dir,turrent_rotate_speed * delta).angle()
if target_dir.dot(my_current_dir) > 0.9 :
shoot()
func _on_range_Area2D_body_entered(body: Node) -> void:
if body.is_in_group("enemy"):
target = body
func _on_range_Area2D_body_exited(body: Node) -> void:
if body == target:
target = null
Perhaps make target be an array instead of a variable.
Then, the tower will attack the first enemy.
When the enemy’s health reaches 0, or the enemy leaves the Area2D…
-If the enemy died, have it emit a signal before dying. The tower should listen for this signal. When the signal is received, pop the first value of the target array.
-If the enemy left the range of the tower, simply pop the first value of said array.
System_Error | 2020-03-23 12:44