Forum begins after the advertisement:


[Part 4] Garlic Border spawning at insane rate

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 4] Garlic Border spawning at insane rate

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #17482
    AoTempest 173
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    Even though I followed everything(except the sprites), for some reason my “Garlic Border” spawns at a fast rate even though I set a cooldownduration. Is there any way to fix this?

    View post on imgur.com
    #17483
    AoTempest 173
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    using UnityEngine;
    
    public class MeleeBehaviour : MonoBehaviour
    {
        public WeaponScriptableObject weaponData;
    
        public float destroyAfterSeconds;
    
        //current stats
        protected float currentDMG;
        protected float currentSpeed;
        protected float currentCoolDown;
        protected int currentPierce;
    
        void Awake()
        {
            currentDMG = weaponData.Damage;
            currentSpeed = weaponData.Speed;
            currentCoolDown = weaponData.CoolDown;
            currentPierce = weaponData.Pierce;
        }
    
        protected virtual void Start()
        {
            Destroy(gameObject, destroyAfterSeconds);
        }
    
        protected virtual void OnTriggerEnter2D(Collider2D col)
        {
            if (col.CompareTag("Enemy"))
            {
                EnemyStats enemy = col.GetComponent<EnemyStats>();
                enemy.TakeDamage(currentDMG);
            }
        }
    }
    #17484
    AoTempest 173
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    public class Garlic_Attack : WeaponController
    {
    
        protected override void Start()
        {
            base.Start();
        }
    
        protected override void Update()
        {
            base.Attack();
            GameObject spawnedBorder = Instantiate(weaponData.Prefab);
            spawnedBorder.transform.position = transform.position;
            spawnedBorder.transform.parent = transform;
        }
    
    }
    #17485
    AoTempest 173
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    using NUnit.Framework;
    using UnityEngine;
    
    public class FaceMaskBehaviour : MeleeBehaviour
    {
        System.Collections.Generic.List<GameObject> markedEnemies;
    
        protected override void Start()
        {
            base.Start();
            markedEnemies = new System.Collections.Generic.List<GameObject>();
        }
    
        protected virtual void OnTriggerEnter2D(Collider2D col)
        {
            if (col.CompareTag("Enemy") && !markedEnemies.Contains(col.gameObject))
            {
                EnemyStats enemy = col.GetComponent<EnemyStats>();
                enemy.TakeDamage(currentDMG);
    
                markedEnemies.Add(col.gameObject);
            }
        }
    }
    #17486
    AoTempest 173
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    using UnityEngine;
    
    public class WeaponController : MonoBehaviour
    {
        [Header("Weapon")]
        public WeaponScriptableObject weaponData;
        float currentcd;
    
        protected Player_Move pm;
    
        protected virtual void Start()
        {
            pm = FindAnyObjectByType<Player_Move>();
            currentcd = weaponData.CoolDown;
        }
    
        // Update is called once per frame
        protected virtual void Update()
        {
            currentcd -= Time.deltaTime;
            if(currentcd <= 0f)
            {
                Attack();
            }
        }
    
        protected virtual void Attack()
        {
            currentcd = weaponData.CoolDown;
        }
    }
    #17487
    AoTempest 173
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    the four scripts were used

    #17488
    Alp Apustaja
    Level 3
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    The issue you are facing with the “Garlic Border” spawning at a fast rate is likely due to the way the Attack() method is implemented in your WeaponController class. Currently, the Attack() method is resetting the currentcd value to the cooldown duration immediately after it’s called, but it seems that the actual instantiation of the “Garlic Border” is being handled in the Update() method, which runs every frame.

    Here’s a breakdown of what you need to adjust:

    1. Modify the Attack() Method: You should instantiate the “Garlic Border” in the Attack() method instead of calling base.Attack() in the Update() method. This way, it will only spawn when the cooldown has expired.

    2. Control the spawning in the Attack() method: You should add the instantiation logic there, ensuring that it only occurs once per cooldown period.

    Here’s how you can modify your Garlic_Attack class:

    public class Garlic_Attack : WeaponController
    {
        protected override void Start()
        {
            base.Start();
        }
    
        protected override void Update()
        {
            // Only check for attack input if cooldown has expired
            if (currentcd <= 0f)
            {
                Attack();
            }
        }
    
        protected override void Attack()
        {
            // Spawn the Garlic Border here
            GameObject spawnedBorder = Instantiate(weaponData.Prefab);
            spawnedBorder.transform.position = transform.position;
            spawnedBorder.transform.parent = transform;
    
            // Reset cooldown
            currentcd = weaponData.CoolDown;
        }
    }

    Key Changes:

    • Spawn Logic: The spawning of the "Garlic Border" is now in the Attack() method.
    • Cooldown Check: The Update() method checks if the cooldown has expired before calling Attack(), ensuring that the attack (and thus, the spawn) only occurs when it's allowed.

    This should resolve the issue of the "Garlic Border" spawning too rapidly. Make sure that weaponData.CoolDown is set correctly in your WeaponScriptableObject for the desired cooldown duration.

    If you have any other questions or need further assistance, feel free to ask!

    #17489
    Alp Apustaja
    Level 3
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It seems like you’re asking for help with some scripts, but I need a little more information to understand what you need. Could you please provide details about the scripts you are using, what they are intended to do, and any specific issues or questions you have regarding them? This will help me assist you better!

    #17490
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    @aotempest173 the code that Alp proposed above should fix your issue.

    For reference, the highlighted portion below is causing the infinite spawn, because Update() runs every frame:

    public class Garlic_Attack : WeaponController
    {
    
        protected override void Start()
        {
            base.Start();
        }
    
        protected override void Update()
        {
            base.Attack();
            GameObject spawnedBorder = Instantiate(weaponData.Prefab);
            spawnedBorder.transform.position = transform.position;
            spawnedBorder.transform.parent = transform;
        }
    
    }
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: