Laser Defender enemy shoots multiple times

I don’t know what I could have done but somewhere along the way my enemies began shooting multiple instances of their lasers. As you can see in the screen shot, sometimes I get one shot sometimes 2 or even 4 … can’t figure it out. Anyone see this before or something wrong with my code?

Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe the minimumFiringRate is lower than expected.

Hope this helps. :slight_smile:


See also:

Noted and I will add the source from now on!

I didn’t even think of getting the original code from the repo – I just did that and copy pasted it and its working fine and not doing what mine is so I will need to figure out where mine differentiates.

Thanks so much for the suggestion!

The code here looks like it matches the course’s code (except for the audio part, but that’s irrelevant).
Did you perhaps define baseFiringRate and firingRateVariance as int? There is a difference between the Random.Range() that takes integers and the Random.Range() that takes floats.
I was thinking the values may just be in a weird range, but since the course’s code works and your code doesn’t, there must be a difference in code

1 Like
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ShooterCOPY : MonoBehaviour

{

    Coroutine firingCoroutine;

    AudioPlayer audioPlayer;

    [Header("General")]

    [SerializeField] GameObject projectilePrefab;

    [SerializeField] float projectileSpeed = 10f;

    [SerializeField] float projectileLifetime = 5f;

    [SerializeField] float baseFiringRate = 0.2f;

    [Header("AI")]

    [SerializeField] bool useAI;

    /*     [SerializeField] float aiShootSpeedLow = 1f;

        [SerializeField] float aiShootSpeedHigh = 4f; */

    [SerializeField] float firingRateVariance = 0f;

    [SerializeField] float minimumFiringRate = 0.1f;

    [HideInInspector] public bool isFiring;

    void Awake()

    {

        audioPlayer = FindObjectOfType<AudioPlayer>();

    }

    void Start()

    {

        if (useAI)

        {

            isFiring = true;

        }

    }

    void Update()

    {

        Fire();

    }

    void Fire()

    {

        if (isFiring && firingCoroutine == null)

        {

            Debug.Log("starting our coroutine");

            firingCoroutine = StartCoroutine(FireContinously());

        }

        else if (!isFiring && firingCoroutine != null)

        {

            Debug.Log("stopping our corouting");

            StopCoroutine(firingCoroutine);

            firingCoroutine = null;

        }

    }

    IEnumerator FireContinously()

    {

        while (true)

        {

            GameObject instance = Instantiate(projectilePrefab, transform.position, Quaternion.identity);

            Rigidbody2D rb = instance.GetComponent<Rigidbody2D>();

            if (rb != null)

            {

                rb.velocity = transform.up * projectileSpeed;

            }

            Destroy(instance, projectileLifetime);

            /*  if (useAI)

             { */

            //firingRate =  Random.Range(1, 4);

            float timeToNextProjectile = Random.Range(baseFiringRate - firingRateVariance, baseFiringRate + firingRateVariance);

            timeToNextProjectile = Mathf.Clamp(timeToNextProjectile, minimumFiringRate, float.MaxValue);

            if (transform.position.x > -5 && transform.position.x < 5)

            {

                audioPlayer.PlayClip(soundTypes.shooting);

            }

            //}

            Debug.Log($"Time to next projectile is {timeToNextProjectile}");

            yield return new WaitForSeconds(timeToNextProjectile);

        }

    }

}

Here’s my exact code @bixarrio above … they were defined as Floats.

What’s your values set to in the inspector? I ran this code and it looks fine to me - but I made assumptions about the values.

Are you sure you are not spawning enemies on top of each other so it looks like one enemy but it’s really 2 or 3 all firing?

1 Like

Thank you for looking into this for me! I thought you were really onto something when you mentioned the enemy spawner but then I remembered that just switching from my shooter script to the one from github made everything work fine.

BUT WHATS REALLY WEIRD is that I just switched that script back to my original one and it’s not doing it anymore. (verified that its my script with debug logs) … so I have no idea. Maybe a glitch in the matrix!?!

Made a diff between mine and the source and I dont see how this could have happened.

image

Perhaps something was just not saved or serialised, but at least it’s working now.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms