So what I’m trying to do is have a triangle constantly calculate the closest piece of “food”, but after a while it stops updating/checking the closest “food” and its position. I will include the code in question along with a screenshot to help you understand my problem.
(Also the “seek()” function in question is in the “update()” function in the script that is attached to the triangle if that has any relevance?)
[110211-untitled.png*_|110211]
private void seek()
{
targets = GameObject.FindGameObjectsWithTag("Food");
for(int i = 0; i < targets.Length; i++)
{
float lastDistance = distBetween(triangleTransform, targets*);*
if (lastDistance < lastClosestDist)
{
lastClosestDist = lastDistance;
closestObject = targets*;*
time = Time.time;
print("Closest Distance: " + lastDistance + " Closest Object: " + closestObject.name);
}
}
}
-----Edit----- Here is the “distBetween()” method for reference(I know this isn’t the problem)
private float distBetween(Transform t, GameObject o)
{
float dx = (t.position.x - o.transform.position.x);
float dy = (t.position.y - o.transform.position.y);
return Mathf.Sqrt((dx * dx) + (dy * dy));
}
_*