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
- This topic has 6 replies, 3 voices, and was last updated 3 weeks, 1 day ago by
Terence.
-
AuthorPosts
-
March 10, 2025 at 9:08 pm #17482::
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
March 10, 2025 at 9:12 pm #17483::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); } } }
March 10, 2025 at 9:13 pm #17484::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; } }
March 10, 2025 at 9:13 pm #17485::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); } } }
March 10, 2025 at 9:14 pm #17486::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; } }
March 10, 2025 at 9:15 pm #17487March 10, 2025 at 9:15 pm #17488::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 yourWeaponController
class. Currently, theAttack()
method is resetting thecurrentcd
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 theUpdate()
method, which runs every frame.Here’s a breakdown of what you need to adjust:
-
Modify the
Attack()
Method: You should instantiate the “Garlic Border” in theAttack()
method instead of callingbase.Attack()
in theUpdate()
method. This way, it will only spawn when the cooldown has expired. -
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 callingAttack()
, 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 yourWeaponScriptableObject
for the desired cooldown duration.If you have any other questions or need further assistance, feel free to ask!
March 10, 2025 at 9:21 pm #17489::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!
March 10, 2025 at 9:40 pm #17490::@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; } }
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: