I am having problems getting nodes to follow the closest relevant node. They behave very weirdly and I think it's probably to do with how I am getting the closest target, explaining's hard so here's some code:
velocity = Vector2().direction_to(closesttarget.position) #* currentspeed
velocity = velocity * 100
and this is the function that gets closesttarget's value:
func _getclosesttarget():
var container = get_parent().get_parent()
var targetsfound = []
for i in container.get_child_count():
targetsfound.append(container.get_child(i).get_child(0))
targetsfound.erase(self) #get rid of self
var closesttarget
var distancetobeat
var indexdistance
var selfglobalposition = transform.origin
var indexglobalposition
for i in range(0, targetsfound.size()):
indexglobalposition = targetsfound[i].position
indexdistance = selfglobalposition.distance_to(indexglobalposition)
if distancetobeat == null :
distancetobeat = indexdistance
closesttarget = targetsfound[i]
else :
if indexdistance < distancetobeat :
distancetobeat = indexdistance
closesttarget = targetsfound[i]
return closesttarget