Hi!
I made a new kind of enemy which shoots 6 (or a custom amount) of projectiles around itself and these projectiles go out in that direction. I managed to program this behaviour and you can even dynamicly set how many projectiles you want to spawn (just change variable in the inspector and the script does everything for you) but there is one problem. The enemy spawns projectiles every frame and I don’t know why. I made the enemy like so: It spawns a projectile just like we did in the course but this projectile spawns 6 (or the custom amount) bombs, set them all in their direction and then it destroys itself. With some debugging I realized that the object get destroyed so the problem is not there, but somewhy the projectile that creates the 6 other projectiles is being instantiated every frame. I have no idea why because it shares the same script as all the other enemies and they work fine. I attached the scripts and the prefabs. ATTACHMENT
Hi Richard,
Looking at your code within Enemy.cs;
// Update is called once per frame
void Update () {
CountDownAndShoot();
}
private void CountDownAndShoot()
{
shotCounter -= Time.deltaTime;
if (shotCounter <= 0f)
{
Fire();
shotCounter = UnityEngine.Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
}
Update
is called every frame, so, have you check your logic within the CountDownAndShoot
method? If your if
statement is always true, then you call Fire
which is presumably responsible for the actual firing that you are seeing.
Try using Debug.Log
within that if
statement and check the frequency that the messages appear in the console, if they are appearing really fast, you’ve identified your problem, then check the values that you are comparing, they are most likely out of the range you were expecting.
Just as a heads up, when it comes to sharing the code, its usually quicker and easier to copy/paste it into your posts and then apply the code formatting characters. Whilst your .unitypackage contained the prefabs also, I had to create a new project/open an existing project to drop your package into, which then errored because it was missing other necessary components - quite time consuming etc, where-as the scripts copy/pasted into the post would speed it up. People will typically ask for more if they are unable to help/work out the issue with what you’ve provided.
Hope this helps
See also;
- Forum User Guides : How to apply code formatting within your post