Problems With Duplicated AI

by vex   Last Updated January 12, 2018 16:13 PM

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.

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

public class NavMeshAI : MonoBehaviour {
public Transform player;
public Transform head;
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);
    }
}
Tags : unity ai


Related Questions


How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM