- Home /
Move to closest object(c#)
I've seen a couple of answers in java script but I don't quite understand them so here goes:
I'm trying to get my squirrel model to move to the closest pine cone. It seems there is a fundamental problem with my script as many times I see the squirrel run past a pine cone right next to it and go accross the screen. When I added five squirrels, they all went for the same cone every time. Here's the script:
public void LookForFood()
{
canMove = true;
eatingTimer = 5;
if(managerScript.pineCones != null)
{
for(int i = 0; i < managerScript.pineCones.Count; i++)
{
if(Vector3.Distance(transform.position, managerScript.pineCones[i].transform.position) < Vector3.Distance(transform.position, managerScript.checkForDistancePineCone))
closestPineCone = managerScript.pineCones[i];
target = managerScript.pineCones[i].transform.position;
}
Quaternion lookAtPineCone = Quaternion.LookRotation(closestPineCone.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, lookAtPineCone, Time.deltaTime);
gameObject.transform.FindChild("SquirrelModel").animation.CrossFade("SquirrelLookFood");
if(gameObject.transform.FindChild("SquirrelModel").animation["SquirrelLookFood"].time > 2.2f)
{
findTarget = true;
squirrelBehaviour = SquirrelBehaviour.MoveToFood;
}
}
else if (managerScript.pineCones == null)
{
squirrelBehaviour = SquirrelBehaviour.FindRandomTree;
}
}
ClosestPineCone is stored in the manager script where the pine cone list is kept. I set this to the first element of the list just as a random check. My hope was that it would check every element of the list and keep comparing to find the shortest distance. I susepct it's breaking out before it does this I can't work out why.
Answer by devstudents · Nov 26, 2014 at 10:10 PM
My mistake (apart from the brackets typo above!) was that I was not changing the value of the checkForDistancePineCone variable -which is really just the first element in the pine cone list. So the for loop would just find the first transform that was closer than pineCones[0] and leave it at that. By changing the checkForDistancePineCone variable to the transform closer to the squirrel, the for loop actually finds the closest object now. Here is the amended code for anyone interested:
for(int i = 0; i < managerScript.pineCones.Count; i++)
{
if(Vector3.Distance(transform.position, managerScript.pineCones[i].transform.position) < Vector3.Distance(transform.position, managerScript.checkForDistancePineCone))
{
managerScript.checkForDistancePineCone = managerScript.pineCones[i].transform.position;
closestPineCone = managerScript.pineCones[i];
}
}
target = closestPineCone.transform.position;
Finally, it's much better to have the checking value a Vector3 (999,999,999). This is a massive value and nothing will ever reach this in your game (I think!). I ran into some bugs when I used a possible location as the check for distance variable.
Answer by TheRaven · Nov 26, 2014 at 05:52 AM
I think it's the 'if statement' on line 9, there are no curly brackets so only the next line is included in the if statement. Is that deliberate?
ie. currently target is set at every loop through the for loop therefore its final value is always the last item in managerScript.pineCones.
there is still definitely something off with the script though... they are still walking past objects.
Your answer
Follow this Question
Related Questions
How do I find the closest target with a tag? c# 2 Answers
Look for closest gameObject and make another gameObject move there. 1 Answer
GameObject.Find closest ?? 2 Answers
Best way to check for supports in building system? 1 Answer
how to find the closest (and second closest) object with tag - one object 4 Answers