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*.transform.position) < Vector3.Distance(transform.position, managerScript.checkForDistancePineCone))*
_ closestPineCone = managerScript.pineCones*;_
_ target = managerScript.pineCones.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.