- Home /
NavMeshAgent object jumps
I tried to set up a script that will have a enemy follow the player using the NavMesh agent whenever the player comes into a certain range and it works the way i want it too but problem seem to happen when i add the same prefab on the NavMesh.
I can't really tell what's happening but it seems the two prefabs with the NavMesh agents on the NavMesh will jump away from each other and then go back to roaming like they should but when they get close they will jump away from each other again. This also happen when i try to drag and drop a prefab with the NavMeshAgent next to another prefab with the component during the game is running, Unity won't let me move it any closer to the other prefab.
This is my script:
// Update is called once per frame
void Update () {
NearPlayer();
Roaming();
}
void NearPlayer()
{
if (Vector3.Distance(transform.position, player.transform.position) <= maxChaseDistance && Vector3.Distance(transform.position, player.transform.position) > maxAttackDistance)
{
inRange = true;
if (inRange)
{
agent.Resume();
agent.SetDestination(player.transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.transform.position - transform.position), moveSpeed * Time.deltaTime);
anim.Play("Chase");
}
}
if (Vector3.Distance(transform.position, player.transform.position) <= maxAttackDistance)
{
agent.Stop();
anim.Play("Attack");
}
if (Vector3.Distance(transform.position, player.transform.position) > maxChaseDistance)
{
inRange = false;
}
}
void Roaming()
{
if (!inRange)
{
timer = timer - Time.deltaTime;
if (timer < 0)
{
timer = 2;
agent.SetDestination(randomDirection);
randomDirection = Random.insideUnitSphere * radius;
randomDirection.y = 0.0f;
randomDirection += startPosition;
}
}
}
I'm guessing due to how unity won't let me drag and drop another prefab near the existing prefab means this isn't really a problem and something about the NavMesh stuff i don't understand.
Would anyone be able to tell me why this is happening and if there is any way around it?