- Edited
I am new to this and am trying to make a 2d monster raiser where bats will fly around and try to kill your (characterbody2d)livestock. Is there a way I could make the bats go to the nearest of your animals?
I am new to this and am trying to make a 2d monster raiser where bats will fly around and try to kill your (characterbody2d)livestock. Is there a way I could make the bats go to the nearest of your animals?
LaggyScripter make an area2d attached to your Sprite for the bat and when a character of a certain name enters the area, the bat moves towards it. You can connect it in the editor or in ready()
. You probably want a value for it to show it has attacked, so it doesn't just stick to the livestock, so use a bool/state/whatever.
Something like:
var _hasAttacked : bool = false
func _on_Area2D_entered(body):
if body.name == "Livestock" and !_hasAttacked:
_whatever_attack_function_you_make()
_hasAttacked = true
Then you can use AnimationPlayer or a PathFollow node to trigger whatever animation.
LaggyScripter are the bats around or coming from the same direction?
if you put the cow scene in a group, you can get all groups and check distance:
var curr_cow : Node2D
var cows : Array = get_tree().get_nodes_in_group("cow")
if cows:
var shortest_distance : float = global_position.distance_to(cows[0].global_position)
curr_cow = cows[0]
for i in cows:
var curr_dist : float = global_position.distance_to(i.global_position)
if curr_dist < shortest_distance:
curr_cow = i
shortest_distance = curr_dist
curr_cow
will then hold the closest node.