Search Unity

Resolved How to only count distance of only one enemy prefab

Discussion in 'Navigation' started by Deleted User, Mar 3, 2023.

  1. Deleted User

    Deleted User

    Guest

    (please correct me if this is the wrong forum) Hello! I have an enemy with a navmesh that is supposed to only attack the player if the player is 5 meters away. Yet they only are stationary if the player is 5 meters away from ALL of the enemies. Here is my enemy class.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.UI;
    6. using static UnityEngine.GraphicsBuffer;
    7. public class Enemy_Master : MonoBehaviour
    8. {
    9.     public GameObject Player;
    10.     //To gather player location
    11.     Rigidbody rb;
    12.     public NavMeshAgent _agent;
    13.     //Required to use the navmesh
    14.     public int Health = 3;
    15.     //counter for enemies health
    16.     public Sprite fullHealth;
    17.     public Sprite halfHeath;
    18.     public Sprite oneHealth;
    19.     public Image HealthBar;
    20.     public AudioClip ZombieDeath;
    21.     static float Distance;
    22.     static public GameObject ThisGameObj;
    23.     static bool isAngered = false;
    24.  
    25.  
    26.  
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.  
    31.         ThisGameObj = gameObject;
    32.         rb = GetComponent<Rigidbody>();
    33.         //gathers rigidbody component
    34.         rb.isKinematic = true;
    35.         Player = GameObject.Find("Player");
    36.         //Finds player
    37.  
    38.  
    39.  
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update()
    44.     {
    45.         if (Health == 0)
    46.         {              
    47.             Destroy(gameObject, 0.2f);
    48.             //kills enemy
    49.         }
    50.         if (Distance <= 5)
    51.         {
    52.         isAngered = true;
    53.         _agent.isStopped = false;
    54.          //Starts the enemy back up again
    55.         }
    56.  
    57.         if (isAngered == true)
    58.         {
    59.             _agent.SetDestination(Player.transform.position);
    60.             //brings the enemie towards the player
    61.         }
    62.  
    63.         else if (isAngered == false)
    64.         {
    65.             _agent.isStopped = true;
    66.             //stops the enemie in its tracks
    67.         }
    68.  
    69.         if (Health == 3)
    70.         {
    71.             HealthBar.sprite = fullHealth;
    72.         }
    73.  
    74.         else if (Health == 2)
    75.         {
    76.             HealthBar.sprite = halfHeath;
    77.         }
    78.  
    79.         else if (Health == 1)
    80.         {
    81.             HealthBar.sprite = oneHealth;
    82.         }
    83.  
    84.         Distance = Vector3.Distance(gameObject.transform.position, ThisGameObj.transform.position);
    85.         //updates distance
    86.     }
    87.  
    88.  
    89.  
    90.     private void OnTriggerEnter(Collider other)
    91.     {
    92.         if (other.gameObject.tag == "Bullet")
    93.         {
    94.             --Health;
    95.             //takes away health when hit by bullet
    96.         }
    97.     }
    98.  
    99.     private void OnDestroy()
    100.     {
    101.         AudioSource.PlayClipAtPoint(ZombieDeath, transform.position);
    102.         //plays the zombie death sound
    103.         //plays the zombie death sound
    104.     }
    105.  
    106.  
    107.  
    108. }
    109.  

    Thanks in advance.:):)
     
  2. Deleted User

    Deleted User

    Guest

  3. Deleted User

    Deleted User

    Guest


    @jasonboukheir3, I have applied your suggestions to my code, but all the enemies are still moving toward the player at once. My updated code is attached to this reply and a video showing this is too. (Enemies are the big yellow guy and tall purple guy.) To watch the video, download the zip file, extract it, and then watch it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.UI;
    6. using static UnityEngine.GraphicsBuffer;
    7. public class Enemy_Master : MonoBehaviour
    8. {
    9.     public GameObject Player;
    10.     //To gather player location
    11.     Rigidbody rb;
    12.     public NavMeshAgent _agent;
    13.     //Required to use the navmesh
    14.     public int Health = 3;
    15.     //counter for enemies health
    16.     public Sprite fullHealth;
    17.     public Sprite halfHeath;
    18.     public Sprite oneHealth;
    19.     public Image HealthBar;
    20.     public AudioClip ZombieDeath;
    21.      float Distance;
    22.      bool isAngered = false;
    23.  
    24.  
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.  
    30.         rb = GetComponent<Rigidbody>();
    31.         //gathers rigidbody component
    32.         rb.isKinematic = true;
    33.         Player = GameObject.Find("Player");
    34.         //Finds player
    35.  
    36.  
    37.  
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.         if (Health == 0)
    44.         {              
    45.             Destroy(gameObject, 0.2f);
    46.             //kills enemy
    47.         }
    48.         if (Distance <= 5)
    49.         {
    50.         isAngered = true;
    51.         _agent.isStopped = false;
    52.          //Starts the enemy back up again
    53.         }
    54.  
    55.         if (isAngered == true)
    56.         {
    57.             _agent.SetDestination(Player.transform.position);
    58.             //brings the enemy towards the player
    59.         }
    60.  
    61.         else if (isAngered == false)
    62.         {
    63.             _agent.isStopped = true;
    64.             //stops the enemy in its tracks
    65.         }
    66.  
    67.         if (Health == 3)
    68.         {
    69.             HealthBar.sprite = fullHealth;
    70.         }
    71.  
    72.         else if (Health == 2)
    73.         {
    74.             HealthBar.sprite = halfHeath;
    75.         }
    76.  
    77.         else if (Health == 1)
    78.         {
    79.             HealthBar.sprite = oneHealth;
    80.         }
    81.  
    82.         Distance = Vector3.Distance(gameObject.transform.position, this.transform.position);
    83.         //updates distance
    84.     }
    85.  
    86.  
    87.  
    88.     private void OnTriggerEnter(Collider other)
    89.     {
    90.         if (other.gameObject.tag == "Bullet")
    91.         {
    92.             --Health;
    93.             //takes away health when hit by a bullet
    94.         }
    95.     }
    96.  
    97.     private void OnDestroy()
    98.     {
    99.         AudioSource.PlayClipAtPoint(ZombieDeath, transform.position);
    100.         //plays the zombie death sound
    101.         //plays the zombie death sound
    102.     }
    103.  
    104.  
    105.  
    106. }
    107.  
    As you can see from the video attached (sorry about the low quality), all the enemies still chase the player. If you would like more information just ask.
     

    Attached Files:

  4. Deleted User

    Deleted User

    Guest

    @jasonboukheir3 I killed all the enemies until there was just one, it turns out that they arent stopping but continuing to chase after the player. What is wrong in my script so that I can fix it?
     
  5. Deleted User

    Deleted User

    Guest

    this line:
    Code (CSharp):
    1. Distance = Vector3.Distance(gameObject.transform.position, this.transform.position);
    this.transform
    returns the same thing as
    gameObject.transform
    . Have you tried debugging those values to see if you get what you expect? Have you tried using
    Debug.Log
    to see what
    Distance
    is?
     
  6. Deleted User

    Deleted User

    Guest

    @jasonboukheir3, I am in gratitude for your kindness while trying to help me with this problem:). But it turns out that I had just made them stop wrong. If anyone has a "similar" problem, here is my enemy class.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.UI;
    6. using static UnityEngine.GraphicsBuffer;
    7. public class Enemy_Master : MonoBehaviour
    8. {
    9.     public GameObject Player;
    10.     //To gather player location
    11.     Rigidbody rb;
    12.     public NavMeshAgent _agent;
    13.     //Required to use the navmesh
    14.     public int Health = 3;
    15.     //counter for enemies health
    16.     public Sprite fullHealth;
    17.     public Sprite halfHeath;
    18.     public Sprite oneHealth;
    19.     public Image HealthBar;
    20.     public AudioClip ZombieDeath;
    21.     float Distance;
    22.     public bool isAngered = false;
    23.     public float wanderRadius = 10f;
    24.     public float wanderTimer = 5f;
    25.     private Transform target;
    26.     private NavMeshAgent agent;
    27.     private float timer;
    28.     public Image Alarmed;
    29.  
    30.  
    31.  
    32.     // Start is called before the first frame update
    33.     void Start()
    34.     {
    35.         Alarmed.enabled = false;
    36.         rb = GetComponent<Rigidbody>();
    37.         //gathers rigidbody component
    38.         rb.isKinematic = true;
    39.         Player = GameObject.Find("Player");
    40.         //Finds player
    41.  
    42.  
    43.  
    44.     }
    45.  
    46.     // Update is called once per frame
    47.     void Update()
    48.     {
    49.         Distance = Vector3.Distance(Player.transform.position, this.transform.position);
    50.         //updates distance
    51.  
    52.         if (Distance <= 8)
    53.         {
    54.             isAngered = true;
    55.         }
    56.  
    57.         if (Distance > 8)
    58.         {
    59.             isAngered = false;
    60.         }
    61.  
    62.         if (isAngered)
    63.         {
    64.             _agent.SetDestination(Player.transform.position);
    65.             Alarmed.enabled = true;
    66.         }
    67.  
    68.         if (!isAngered)
    69.         {
    70.          // For wandering, ignore things after this if you are only concerned with the distance and navmesh AI
    71.             Alarmed.enabled = false;
    72.  
    73.             timer += Time.deltaTime;
    74.  
    75.             if (timer >= wanderTimer)
    76.             {
    77.                 Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1);
    78.                 agent.SetDestination(newPos);
    79.                 timer = 0;
    80.             }
    81.         }
    82.  
    83.  
    84.         HealthChange();
    85.  
    86.         Physics.IgnoreLayerCollision(3, 2);
    87.  
    88.  
    89.  
    90.     }
    91.  
    92.  
    93.  
    94.     private void OnTriggerEnter(Collider other)
    95.     {
    96.         if (other.gameObject.tag == "Bullet" || isAngered == false)
    97.         {
    98.             --Health;
    99.             //takes away health when hit by bullet
    100.         }
    101.     }
    102.  
    103.     private void OnDestroy()
    104.     {
    105.         AudioSource.PlayClipAtPoint(ZombieDeath, transform.position);
    106.         //plays the zombie death sound
    107.     }
    108.  
    109.  
    110.     void HealthChange()
    111.     {
    112.         if (Health == 3)
    113.         {
    114.             HealthBar.sprite = fullHealth;
    115.         }
    116.  
    117.         else if (Health == 2)
    118.         {
    119.             HealthBar.sprite = halfHeath;
    120.         }
    121.  
    122.         else if (Health == 1)
    123.         {
    124.             HealthBar.sprite = oneHealth;
    125.         }
    126.  
    127.         if (Health == 0)
    128.         {
    129.  
    130.             Destroy(gameObject, 0.2f);
    131.             //kills enemy
    132.         }
    133.  
    134.     }
    135.  
    136.     void OnEnable()
    137.     {
    138.         agent = GetComponent<NavMeshAgent>();
    139.         timer = wanderTimer;
    140.     }
    141.  
    142.  
    143.  
    144.     public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
    145.     {
    146.         Vector3 randDirection = Random.insideUnitSphere * dist;
    147.  
    148.         randDirection += origin;
    149.  
    150.         NavMeshHit navHit;
    151.  
    152.         NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);
    153.  
    154.         return navHit.position;
    155.     }
    156. }
    157.  
     
    Deleted User likes this.