Search Unity

navmeshAI block collisions for two cloned Enemy Objects

Discussion in 'Navigation' started by freedom667, Apr 22, 2017.

  1. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    I have a problem about enemies. I want to block collision between enemies. So i have to put distance for cloned object but i couldn't do it. can you help me for this?



    My code is below:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class zombie_ai : MonoBehaviour {
    7.  
    8.     public float distance;
    9.     public AudioClip[] moaning;
    10.     public float sec = 3;
    11.  
    12.     private Transform player;
    13.     private PlayerHealth playerHealth;
    14.     private EnemyHealth enemyHealth;
    15.     private ZombieAttack attack;
    16.     private NavMeshAgent nav;
    17.     private Animator anim;
    18.     private float timer;
    19.     private float sum;
    20.  
    21.     bool play = true;
    22.  
    23.     AudioSource moanAudio;
    24.  
    25.     void Start () {
    26.         moanAudio = GetComponent<AudioSource> ();
    27.         player = GameObject.FindGameObjectWithTag("Player").transform;
    28.         enemyHealth = GetComponentInChildren<EnemyHealth> ();
    29.         playerHealth = player.GetComponent<PlayerHealth> ();
    30.         anim = GetComponent<Animator> ();
    31.         nav = GetComponent<NavMeshAgent> ();
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     void Update () {
    36.         if (play) {
    37.             StartCoroutine ("Sound");
    38.         }
    39.         if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0) {
    40.             nav.SetDestination (player.position);
    41.         } else {
    42.             nav.enabled = false;
    43.             anim.SetTrigger ("Eating");
    44.         }
    45.  
    46.         if (Vector3.Distance (player.position, transform.position) <= distance) {
    47.             Debug.Log ("Attacking");  
    48.             anim.SetTrigger ("Attack");
    49.         }
    50.         else {
    51.             anim.Play ("Zombie_Walk");
    52.         }
    53.  
    54.     }
    55.  
    56.     IEnumerator Sound()
    57.     {
    58.         play = false;
    59.         sum += sec;
    60.         int i = Random.Range(0, moaning.Length);
    61.         Debug.Log ("caldi");
    62.         AudioSource.PlayClipAtPoint(moaning[i], transform.position);
    63.         yield return new WaitForSeconds (sec);
    64.         play = true;
    65.     }
    66.  
    67. }
    68.  
     
  2. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425