Search Unity

Bug Trying to put multiple guns on an aircraft creates weird behavior

Discussion in 'Editor & General Support' started by Jivu286, Dec 11, 2023.

  1. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    Hello,

    I'm currently using a script that spawns a bullet at a location to fire aircraft cannons. The bullet then contains a script for its speed, spread, damage, etc. The hope was that this would let me reuse the same scripts and assign different values to simulate firing mixed caliber weapons.

    This has been working fine when I have multiple firing points for bullets of the same type, but now when I try to add firing points for bullets of a different type, some weird behaviors happen. It will work as intended for the first few shots, but then the two types of guns fall out of "sync". They're supposed to have different fire rates, but they're also supposed to fire at the same time and always fire immediately on click. Instead there is a little lag between clicking and firing and the bullets will only fire one "type" at a time.

    So far I have tried to see if I can unblock some kind of bottleneck by creating extra copies of the firing script and the bullet script to assign to the secondary caliber guns, but this produced no changes. Next I suppose is to look into the firerate timer I'm using,

    This is the current logic:


    Code (CSharp):
    1. private void FixedUpdate()
    2. {
    3.  
    4.     if (Input.GetMouseButton(0))
    5.     {
    6.         timer += Time.deltaTime;
    7.         if (timer >= FireRate)
    8.         {
    9.             timer = 0;
    10.             GameObject go1 = Instantiate(BulletPrefab, FirePoint.transform.position, FirePoint.transform.rotation);
    11.             go1.SetActive(true);      
    12.             MuzzleFlash.SetActive(true);
    13.             playGunShot();
    14.  
    15.         }
    Any ideas of what's going wrong or how to fix it?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,682
  3. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    So I've worked everything out, the problem was that I was instantiating too much which lagged fire rate and also bullets were colliding with each other. I fixed it by implementing a pooling system for bullets and making them ignore collision on a "bullet" layer
     
    Kurt-Dekker likes this.