Search Unity

How can I shoot all the bullets depending on number of npcs and fire points ?

Discussion in 'Scripting' started by dubiduboni_unity, May 2, 2019.

  1. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    Each npc have a weapon and the weapon have two fire points.
    Each time I click the mouse left button it should make shooting. And shoot from each fire point a bullet.

    So if I have for example in the npcList two npcs and in the list firePoints 4 items two firePoints from each npc weapon it should shooting 4 bullets.

    The problem is for example if I'm looping over the npcList and calling the LaunchProjectile method twice.
    But in this case there are 4 firePoints. Two from the first npc in the npcList and two from the second npc in the npcList. So it should shoot 4 bullets but it's shooting only 2.

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Shooting : MonoBehaviour
    8. {
    9.     [Header("Main")]
    10.     public List<GameObject> npcList;
    11.     public Transform[] firePoints;
    12.     public Rigidbody bulletPrefab;
    13.     public float launchForce = 700f;
    14.     public bool automaticFire = false;
    15.     public float bulletDestructionTime;
    16.  
    17.     [Space(5)]
    18.     [Header("Slow Down")]
    19.     public float maxDrag;
    20.     public float bulletSpeed;
    21.     private bool bulletsSlowDown = false;
    22.     public bool overAllSlowdown = false;
    23.     [Range(0, 1f)]
    24.     public float slowdownAll = 1f;
    25.  
    26.     private List<Animator> anims = new List<Animator>();
    27.  
    28.     private void Start()
    29.     {
    30.         for (int i = 0; i < npcList.Count; i++)
    31.         {
    32.             anims.Add(npcList[i].GetComponent<Animator>());
    33.             anims[i].SetBool("Shooting", true);
    34.         }
    35.     }
    36.  
    37.     public void Update()
    38.     {
    39.         if (overAllSlowdown == true)
    40.         {
    41.             Time.timeScale = slowdownAll;
    42.         }
    43.  
    44.         for(int i = 0; i < npcList.Count; i++)
    45.         {
    46.             if (isAnimationStatePlaying(anims[i], 0, "AIMING") == true)
    47.             {
    48.                 if (Input.GetButtonDown("Fire1") && automaticFire == false)
    49.                 {
    50.                     if (anims[i].GetBool("Shooting") == true)
    51.                     {
    52.                         anims[i].Play("SHOOTING");
    53.                         LaunchProjectile(firePoints[i]);
    54.                     }
    55.                 }
    56.                 else if (Input.GetButtonDown("Fire1") && automaticFire == true)
    57.                 {
    58.                     automaticFire = false;
    59.                 }
    60.                 else
    61.                 {
    62.                     if (Input.GetButtonDown("Fire2"))
    63.                     {
    64.                         automaticFire = true;
    65.                     }
    66.                     if (automaticFire == true)
    67.                     {
    68.                         anims[i].Play("SHOOTING");
    69.                         LaunchProjectile(firePoints[i]);
    70.                     }
    71.                 }
    72.             }
    73.         }
    74.     }
    75.  
    76.     private void LaunchProjectile(Transform firePoint)
    77.     {
    78.         Rigidbody projectileInstance = Instantiate(
    79.             bulletPrefab,
    80.             firePoint.position,
    81.             firePoint.rotation);
    82.  
    83.         projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);
    84.  
    85.         if (bulletsSlowDown == true)
    86.         {
    87.             if (projectileInstance != null)
    88.             {
    89.                 StartCoroutine(AddDrag(maxDrag, bulletSpeed, projectileInstance));
    90.             }
    91.         }
    92.  
    93.         if ((automaticFire == true || automaticFire == false) && bulletsSlowDown == false)
    94.         {
    95.             projectileInstance.gameObject.AddComponent<BulletDestruction>().destructionTime = bulletDestructionTime;
    96.             projectileInstance.gameObject.GetComponent<BulletDestruction>().Init();
    97.         }
    98.     }
    99.  
    100.     IEnumerator AddDrag(float maxDrag, float bulletSpeed, Rigidbody rb)
    101.     {
    102.         if (rb != null)
    103.         {
    104.             float current_drag = 0;
    105.  
    106.             while (current_drag < maxDrag)
    107.             {
    108.                 current_drag += Time.deltaTime * bulletSpeed;
    109.                 rb.drag = current_drag;
    110.                 yield return null;
    111.             }
    112.  
    113.             rb.velocity = Vector3.zero;
    114.             rb.angularVelocity = Vector3.zero;
    115.             rb.drag = 0;
    116.  
    117.             rb.gameObject.AddComponent<BulletDestruction>().destructionTime = bulletDestructionTime;
    118.             rb.gameObject.GetComponent<BulletDestruction>().Init();
    119.         }
    120.     }
    121.  
    122.     bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName)
    123.     {
    124.         if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
    125.             return true;
    126.         else
    127.             return false;
    128.     }
    129. }
    130.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're using the same index for your firePoints as npcList, so I'm surprised you're not getting errors when those lists don't just happen to match in size. Pretty sure you want to replace your current LaunchProjectile calls from:

    Code (csharp):
    1.  
    2. LaunchProjectile(firePoints[i]);
    3.  
    to:

    Code (csharp):
    1.  
    2. foreach(Transform firePoint in firePoints)
    3.      LaunchProjectile(firePoint);
    4.  
    That will loop through all of your firePoints and launch a projectile from them every time you tell it to fire instead.
     
    dubiduboni_unity likes this.
  3. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116

    Still not working. This is the script after the changes :

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Shooting : MonoBehaviour
    8. {
    9.     [Header("Main")]
    10.     public List<GameObject> npcList;
    11.     public Transform[] firePoints;
    12.     public Rigidbody bulletPrefab;
    13.     public float launchForce = 700f;
    14.     public bool automaticFire = false;
    15.     public float bulletDestructionTime;
    16.  
    17.     [Space(5)]
    18.     [Header("Slow Down")]
    19.     public float maxDrag;
    20.     public float bulletSpeed;
    21.     private bool bulletsSlowDown = false;
    22.     public bool overAllSlowdown = false;
    23.     [Range(0, 1f)]
    24.     public float slowdownAll = 1f;
    25.  
    26.     private List<Animator> anims = new List<Animator>();
    27.  
    28.     private void Start()
    29.     {
    30.         for (int i = 0; i < npcList.Count; i++)
    31.         {
    32.             anims.Add(npcList[i].GetComponent<Animator>());
    33.             anims[i].SetBool("Shooting", true);
    34.         }
    35.     }
    36.  
    37.     public void Update()
    38.     {
    39.         if (overAllSlowdown == true)
    40.         {
    41.             Time.timeScale = slowdownAll;
    42.         }
    43.  
    44.         for (int i = 0; i < npcList.Count; i++)
    45.         {
    46.             if (isAnimationStatePlaying(anims[i], 0, "AIMING") == true)
    47.             {
    48.                 if (Input.GetButtonDown("Fire1") && automaticFire == false)
    49.                 {
    50.                     if (anims[i].GetBool("Shooting") == true)
    51.                     {
    52.                         anims[i].Play("SHOOTING");
    53.                         //LaunchProjectile(firePoints[i]);
    54.                         foreach (Transform firePoint in firePoints)
    55.                             LaunchProjectile(firePoint);
    56.                     }
    57.                 }
    58.                 else if (Input.GetButtonDown("Fire1") && automaticFire == true)
    59.                 {
    60.                     automaticFire = false;
    61.                 }
    62.                 else
    63.                 {
    64.                     if (Input.GetButtonDown("Fire2"))
    65.                     {
    66.                         automaticFire = true;
    67.                     }
    68.                     if (automaticFire == true)
    69.                     {
    70.                         anims[i].Play("SHOOTING");
    71.                         //LaunchProjectile(firePoints[i]);
    72.                         foreach (Transform firePoint in firePoints)
    73.                             LaunchProjectile(firePoint);
    74.                     }
    75.                 }
    76.             }
    77.         }
    78.     }
    79.  
    80.     private void LaunchProjectile(Transform firePoint)
    81.     {
    82.         Rigidbody projectileInstance = Instantiate(
    83.             bulletPrefab,
    84.             firePoint.position,
    85.             firePoint.rotation);
    86.  
    87.         projectileInstance.AddForce(new Vector3(0, 0, 1) * launchForce);
    88.  
    89.         if (bulletsSlowDown == true)
    90.         {
    91.             if (projectileInstance != null)
    92.             {
    93.                 StartCoroutine(AddDrag(maxDrag, bulletSpeed, projectileInstance));
    94.             }
    95.         }
    96.  
    97.         if ((automaticFire == true || automaticFire == false) && bulletsSlowDown == false)
    98.         {
    99.             projectileInstance.gameObject.AddComponent<BulletDestruction>().destructionTime = bulletDestructionTime;
    100.             projectileInstance.gameObject.GetComponent<BulletDestruction>().Init();
    101.         }
    102.     }
    103.  
    104.     IEnumerator AddDrag(float maxDrag, float bulletSpeed, Rigidbody rb)
    105.     {
    106.         if (rb != null)
    107.         {
    108.             float current_drag = 0;
    109.  
    110.             while (current_drag < maxDrag)
    111.             {
    112.                 current_drag += Time.deltaTime * bulletSpeed;
    113.                 rb.drag = current_drag;
    114.                 yield return null;
    115.             }
    116.  
    117.             rb.velocity = Vector3.zero;
    118.             rb.angularVelocity = Vector3.zero;
    119.             rb.drag = 0;
    120.  
    121.             rb.gameObject.AddComponent<BulletDestruction>().destructionTime = bulletDestructionTime;
    122.             rb.gameObject.GetComponent<BulletDestruction>().Init();
    123.         }
    124.     }
    125.  
    126.     bool isAnimationStatePlaying(Animator anim, int animLayer, string stateName)
    127.     {
    128.         if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName))
    129.             return true;
    130.         else
    131.             return false;
    132.     }
    133. }
    134.  
    And this is a screenshot of the Shooting script settings in the Inspector :



    There are 4 fire points. It should shooting (I mean what I want it to do) 4 bullets each time. But it's shooting 8 bullets :

     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You have 2 NPCs in the list, and 4 FirePoints. 2 x 4 is 8 bullets... so if you want each NPC to fire individually, you're going to have to change up the logic of your script. Right now, any NPC that isn't shooting will shoot when you click the mouse.
     
    dubiduboni_unity likes this.