Search Unity

[Solved]Group of enemies ai moving all in different direction with Random.Range, I need help

Discussion in 'Scripting' started by Pixitales, May 19, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    I want to make a game like Infectonator, where all the zombies randomly moves around in all different directions. So far, all the zombies move in the same direction together with Random.Range. How to get each zombie move in a different direction?

    Sorry, my zombie script is really long.... All the character health and animator is on my NPC scripts. I think you can still make this work without animation stuff. Just change NPC to MonoBehaviour. This uses IState, but I crossed them out so you guys can try it out.

    Here is a short video of the problem:
    https://media.giphy.com/media/kCzrRYCF6YxYCWIk7f/giphy.gif

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Zombie : NPC
    6. {
    7.     public List<GameObject> allTargets = new List<GameObject>();
    8.     public List<GameObject> allZombies = new List<GameObject>();
    9.  
    10.     private GameObject[] humanTargets;
    11.     private GameObject[] zombies;
    12.  
    13.     private int index;
    14.  
    15.     //public Transform Mytarget;
    16.  
    17.     //private Vector2 direction;
    18.  
    19.     private float lineOfSight = 5f;
    20.     public float LineOfSight { get => lineOfSight; set => lineOfSight = value; }
    21.  
    22.     private float attackDistance = 1f;
    23.     public float AttackDistance { get => attackDistance; set => attackDistance = value; }
    24.  
    25.     private float chaseSpeed = 0.5f;
    26.     public float ChaseSpeed { get => chaseSpeed; set => chaseSpeed = value; }
    27.  
    28.     private float distanceToTarget;
    29.     public float DistanceToTarget { get => distanceToTarget; set => distanceToTarget = value; }
    30.  
    31.  
    32.     public bool isAttacking = false;
    33.     public bool isMoving = true;
    34.     public bool isPatrolling = true;
    35.  
    36.     private float TimerForNextAttack = 3f;
    37.     private float attackCooldown = 3f;
    38.  
    39.     public Transform moveSpot;
    40.     public float minX;
    41.     public float maxX;
    42.     public float minY;
    43.     public float maxY;
    44.  
    45.     private float waitTime;
    46.     private float startWaitTime;
    47.  
    48.     private IZombieState currentState;
    49.  
    50.  
    51.     protected void Awake()
    52.     {
    53.         //ChangeState(new IdleZombieState());
    54.  
    55.         waitTime = startWaitTime;
    56.  
    57.         moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
    58.  
    59.         Direction = (moveSpot.transform.position - transform.position).normalized;
    60.     }
    61.  
    62.     protected override void Update()
    63.     {
    64.         //currentState.Update();
    65.  
    66.         //Access to NPC script
    67.         base.Update();
    68.  
    69.         CheckForTargets();
    70.     }
    71.  
    72.     public void CheckForTargets()
    73.     {
    74.         allTargets.Clear();
    75.         allZombies.Clear();
    76.  
    77.         //Check if the zombie is still alive
    78.         if (IsAlive && !isAttacking)
    79.         {
    80.             //Find all Humans with a tag name and add to target list
    81.             humanTargets = GameObject.FindGameObjectsWithTag("Enemy");
    82.             for (int i = 0; i < humanTargets.Length; i++)
    83.             {
    84.                 allTargets.Add(humanTargets[i]);
    85.             }
    86.  
    87.             //Find all Zombies with a tag name and add to target list
    88.             zombies = GameObject.FindGameObjectsWithTag("Zombie");
    89.             for (int i = 0; i < zombies.Length; i++)
    90.             {
    91.                 allZombies.Add(zombies[i]);
    92.             }
    93.  
    94.             //Check through the list of all targets
    95.             if (allTargets.Count > 0)
    96.             {
    97.                 //Scroll through all the targets for the best one to target
    98.                 for (int i = 0; i < allTargets.Count; i++)
    99.                 {          
    100.                     //Determine how far the Zombie is away from the target
    101.                     float DistanceToTarget = Vector2.Distance(transform.position, allTargets[i].transform.position);
    102.                     //Debug.Log("All Targets - " + allTargets[i] + "distance " + targetDistance);
    103.  
    104.                     //If the target is within range
    105.                     if (DistanceToTarget <= LineOfSight)
    106.                     {
    107.                         //Lock on target
    108.                         MyTarget = allTargets[i].transform;
    109.  
    110.                         //If not using IState, use this below:
    111.                         FollowTarget();
    112.  
    113.                         //If target is in range to attack
    114.                         if (DistanceToTarget <= AttackDistance)
    115.                         {
    116.                             isMoving = false;
    117.  
    118.                             StartCoroutine(Attack());
    119.                         }
    120.                         else
    121.                         {
    122.                             isMoving = true;
    123.                             MyAnimator.ResetTrigger("attack");
    124.                         }
    125.                     }
    126.  
    127.                     //If target is out of range
    128.                     else if (DistanceToTarget >= LineOfSight)
    129.                     {
    130.                         //Remove target
    131.                         MyTarget = null;
    132.  
    133.                         Patrol();
    134.  
    135.                         //Stops moving
    136.                         //Direction = Vector2.zero;
    137.                     }
    138.                 }
    139.             }
    140.             else if(GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
    141.             {
    142.                 Patrol();
    143.             }
    144.         }
    145.     }
    146.  
    147.     public void Patrol()
    148.     {
    149.         //Get a random index number
    150.         //index = Random.Range(0, 5);
    151.  
    152.         //Move towards moveSpot
    153.         transform.position = Vector2.MoveTowards(transform.position, moveSpot.position, MoveSpeed * Time.deltaTime);
    154.  
    155.         //Face the correct direction
    156.         Direction = (moveSpot.transform.position - transform.position).normalized;
    157.  
    158.         //Wait time for next move
    159.         if (Vector2.Distance(transform.position, moveSpot.position) < 0.2f)
    160.         {
    161.             if (waitTime <= 0)
    162.             {
    163.                 moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
    164.  
    165.                 waitTime = startWaitTime;
    166.             }
    167.             else
    168.             {
    169.                 waitTime -= Time.deltaTime;
    170.             }
    171.         }
    172.     }
    173.  
    174.     public void FollowTarget()
    175.     {
    176.         if (isMoving)
    177.         {
    178.             //Face the correct direction
    179.             Direction = (MyTarget.transform.position - transform.position).normalized;
    180.  
    181.             //Move towards target
    182.             transform.position = Vector2.MoveTowards(transform.position, MyTarget.position, ChaseSpeed * Time.deltaTime);
    183.         }
    184.     }
    185.  
    186.     public IEnumerator Attack()
    187.     {
    188.         if (MyTarget != null)
    189.         {
    190.             isAttacking = true;
    191.  
    192.             MyAnimator.SetTrigger("attack");
    193.  
    194.             Direction = Vector2.zero;
    195.  
    196.             yield return new WaitForSeconds(MyAnimator.GetCurrentAnimatorStateInfo(2).length);
    197.  
    198.             Debug.Log("Enemy attacking");
    199.  
    200.             isAttacking = false;
    201.         }
    202.     }
    203.  
    204.     public void ChangeState(IZombieState newState)
    205.     {
    206.         if (currentState != null)
    207.         {
    208.             currentState.Exit();
    209.         }
    210.  
    211.         currentState = newState;
    212.  
    213.         currentState.Enter(this);
    214.     }
    215. }
    216.  
     
    Last edited: May 19, 2019
  2. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Problem solved! I was using a bad code for the partrol. I have to use my other method. It happens sometimes when watching beginner tutorials.