# AI Duplication Issues

## Recommended Posts

I am having a problem with making duplicates of my AI. When one is within range, it performs normally. If two are in range, then they both freeze up. The AI are direct duplicates of each other. I don't know what causes this. Code below:

Any help is seriously appreciated. Thank you.

Code Below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NavMeshAI : MonoBehaviour {
public Transform player;
public Animator anim;
public bool pursuing = false;
public NavMeshAgent navComponent;
public Transform enemy;

// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
navComponent = this.gameObject.GetComponent<NavMeshAgent>();
}

// Update is called once per frame
void Update () {
Vector3 direction = player.position - this.transform.position;
direction.y = 0;

float angle = Vector3.Angle (direction, head.up);

if (Vector3.Distance (player.position, this.transform.position) < 10 && (angle < 360 || pursuing)) {

pursuing = true;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);

anim.SetBool ("isIdle", false);
if (direction.magnitude > 2) {
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
if (pursuing == true) {
navComponent.SetDestination (player.position);
}
} else {
anim.SetBool ("isAttacking", true);
anim.SetBool ("isWalking", false);
}
} else {
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
pursuing = false;
navComponent.SetDestination (enemy.position);
}
}

##### Share on other sites

It's not clear what all your public variables are, so it's hard to be sure. If 'enemy' was the same character as the one doing the AI, its easy to see how the last line of navigation would actually do nothing, for example.

Use Debug.Log lines to see which code branches are being executed, and which values the code has at that time.

##### Share on other sites

The first thing that looks suspicious to me is the Vector3.Angle.  According to the documentation:

"The angle returned is the unsigned acute angle between the two vectors. This means the smaller of the two possible angles between the two vectors is used. The result is never greater than 180 degrees."

It looks like you're measuring the angle between the direction between you and the player vs. the "up" direction of your head.  I suppose if your head transform's "up" is supposed to be facing towards the player this might be useful but it's a bit weird.

But then you use it with "angle < 360".  The Vector3.Angle documentation says that the angle returned cannot be greater than 180 degrees.  So this is always true, and that condition won't contribute to the code working or not.  So we can eliminate that from the things that might cause your bug.

The rest of the code doesn't look like it has anything that might influence a different instance.

You say you "duplicate" your AI?  Can you show us the code that does that?  I think the problem is related to that, or at the very least not in the code you posted above.

Also, what is 'enemy' assigned to?  Is it the player?  Is it the AI?  Is it a third object?

Edited by Nypyren

##### Share on other sites

I commented out your Animation code and it worked fine.

So it is either something wrong with your animation or it is the way you duplicate. I just right clicked and selected "Duplicate" this also fills the script with the correct targets to each AI.

I just used a quick file I had.

The code:

Spoiler


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NavMeshAI : MonoBehaviour {
public Transform player;
//public Animator anim;
public bool pursuing = false;
public NavMeshAgent navComponent;
public Transform enemy;

// Use this for initialization
void Start () {
//anim = GetComponent<Animator> ();
navComponent = this.gameObject.GetComponent<NavMeshAgent>();
}

// Update is called once per frame
void Update () {
Vector3 direction = player.position - this.transform.position;
direction.y = 0;

float angle = Vector3.Angle (direction, head.up);

if (Vector3.Distance (player.position, this.transform.position) < 10 && (angle < 360 || pursuing)) {

pursuing = true;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);

//anim.SetBool ("isIdle", false);
if (direction.magnitude > 2) {
this.transform.Translate (0, 0, 0.05f);
//anim.SetBool ("isWalking", true);
//anim.SetBool ("isAttacking", false);
if (pursuing == true) {
navComponent.SetDestination (player.position);
}
}
} else {
//anim.SetBool ("isIdle", true);
//anim.SetBool ("isWalking", false);
//anim.SetBool ("isAttacking", false);
pursuing = false;
navComponent.SetDestination (enemy.position);
}
}
}

## Create an account

Register a new account

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
13
5. 5

• 9
• 14
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007359

×