I am currently implementing a sheep AI into my game but pretty soon they all choose to go to the same spot, i can’t for the life of me work out what is wrong as i am reading the output for the random number and it works fine for about 20 seconds then it just defaults to 94… Just for clarification i have a master gameobject with about 150 children that are possible points for the ai to go to.
Any help is appreciated XD
A Video Of The Issue Can Be Found Here: Unity Issue - YouTube
My Sheep Controller Code:
enum PossibleActions { Idle, Eating, Walking, RunningForExit }
PossibleActions currentAction;
GameObject locations;
NavMeshAgent agent;
public float timeLeftOfIdle, timeLeftOfWalking, timeLeftOfEating = 10f;
public bool doAi = false;
public bool doAnim = true;
public bool forceMove = false;
public Animator anim;
// Use this for initialization
void Start()
{
if (!(GameInfo.Instance.isHost)) { GetComponent<NavMeshAgent>().enabled = false; this.enabled = false; }
anim = GetComponentInChildren<Animator>();
locations = GameInfo.Instance.SheepNavigationLocations;
agent = GetComponent<NavMeshAgent>();
Debug.Log("Start Method Calling For " + transform.name);
currentAction = pickAction(true);
pickLocation();
}
// Update is called once per frame
void Update()
{
if (doAi)
{
if (currentAction == PossibleActions.Eating)
{
timeLeftOfEating -= Time.deltaTime;
if (timeLeftOfEating <= 0.1f)
{
//Eating time limit hit pick a new task
currentAction = pickAction(forceMove);
}
}
else if (currentAction == PossibleActions.Idle)
{
timeLeftOfIdle -= Time.deltaTime;
if (timeLeftOfIdle <= 0.1f)
{
//Idle time limit hit pick a new task
currentAction = pickAction(forceMove);
}
}
else if (currentAction == PossibleActions.Walking)
{
timeLeftOfWalking -= Time.deltaTime;
if (timeLeftOfWalking <= 0.1f)
{
//Walking time limit hit pick a new task
currentAction = pickAction(forceMove);
}
else if (!agent.pathPending)
{
if (agent.remainingDistance <= agent.stoppingDistance)
{
if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
{
//Reached destination pick a new task
Debug.Log(transform.name + " Sheep Reached Its Destination");
currentAction = pickAction(forceMove);
}
}
}
}
else if (currentAction == PossibleActions.RunningForExit)
{
currentAction = pickAction(forceMove);
}
}
}
PossibleActions pickAction(bool forceMove = false)
{
timeLeftOfWalking = Random.Range(5, 25);
agent.SetDestination(pickLocation());
WalkAnim();
Debug.Log("Walking For " + transform.name + "Forcemove: " + forceMove);
return PossibleActions.Walking;
}
Vector3 pickLocation()
{
int childNum = Random.Range(0, locations.transform.childCount - 1);
Debug.Log("Picked Num:" + childNum);
return locations.transform.GetChild(childNum).transform.position;
}