- Home /
delaying the start of a navmesh agent
Hi. I am making a 1st person version of pacman and I'm having trouble with the ghost ai. I've based the way they navigate the maze on the original namco arcade game.
The problem I'm having is when the ghosts start to move and what "mode" they are in. I have variables set up so that each of the 4 ghosts start when a certain amount of dots have been eaten. 2 of them are set to 0, 1 is set to 30 and the 4th is set to 60. This works as intended, but the "mode" also doesn't start until the ghost does. I only have 2 of the 3 ghost modes so far, "chase" and "scatter". "Frightened" will come later and will no doubt be a whole new problem!
For simplicity, the modes always last the same amount of time...7 seconds of "scatter" followed by 20 seconds of "chase" and repeat. The 2 ghosts that start on 0 dots, start in "scatter" mode. If I eat 30 dots when they are in "chase" mode, the 3rd ghost will become active and be in "scatter" mode when he should be in the same mode as the others.
So, how would I go about delaying the start of a ghost but keep his mode in line with already active ghosts?
I shall keep plugging away trying things but chopping things out of 1 script and putting in another is confusing me and just breaks everything :D
my ghost code.... ignore "ghostMode", that is for when I do the "frightened" mode
using UnityEngine;
using System.Collections;
public class GhostAI : MonoBehaviour
{
public static int ghostDots;
public int startDots;
public Transform chase;
public Transform scatter;
private int ghostMode = 1;
public int chaseSeconds = 20;
public int scatterSeconds = 7;
private float currentSeconds = 0f;
private NavMeshAgent agent;
void Start ()
{
agent = gameObject.GetComponent<NavMeshAgent>();
}
void Update()
{
ghostDots = EatingScript.dotCount;
currentSeconds += Time.deltaTime;
if (ghostDots > startDots) {
if (currentSeconds < scatterSeconds && ghostMode == 1) {
agent.SetDestination (scatter.position);
//Debug.Log("Scatter Mode");
}
if (currentSeconds > scatterSeconds && ghostMode == 1) {
agent.SetDestination (chase.position);
//Debug.Log("Chase Mode");
}
if (currentSeconds > chaseSeconds && ghostMode == 1) {
currentSeconds = 0;
}
}
}
}
Answer by MitchWardle180 · Jun 04, 2015 at 07:26 AM
It may be happening because all of the monsters have there own script, maybe create a "MonsterManager" script which handles the amount of monsters in an array or list and also which mode they are in. This would allow you to have functions to retrieve information about the monster or mosters whilst keeping them in the same state
Hope this helped
Thanks, you could be right, but everything else worked at the time of asking and as a beginner, I'm confused by the use of arrays, and so didn't want to drastically change everything. I may have to I suppose, as I implemented "Frightened" mode recently and couldn't get that to work properly. They went into frightened mode, but it all messed up when I tried to eat one :D Frightened mode was handled by the players "eating" script. I moved it to the ghosts AI script and I'm now getting extremely frustrated, as frightened mode only ever gets applied to one of the ghosts. At one point I managed to get all 4 into frightened, but unable to get more than one to go back to not frightened. I'm about to post another question :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613200551im_/https://answers.unity.com/themes/thub/images/avi.jpg)