Search Unity

Spawning after amount of time without spamming

Discussion in 'Scripting' started by jtr912, Jan 15, 2021.

Thread Status:
Not open for further replies.
  1. jtr912

    jtr912

    Joined:
    Jan 15, 2021
    Posts:
    2
    Hello there fellow Unityers!

    I have been working on a small game that is kind of similar to space invaders.

    Instead, my player shoots dogs at balls (see video). The dogs are spawned by pressing my enter bar.



    My only problem is that I want my player to only spawn a new dog after a certain amount of time has passed to prevent "spamming" the enter bar. I have been having trouble with this and I was wondering if someone knowledgeable knows where to start with this. I have been googling like crazy and I thought it would be time to ask for some help. Thank you in advance.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Timers;
    4. using UnityEngine;
    5.  
    6. public class PlayerControllerX : MonoBehaviour
    7. {
    8.     public GameObject dogPrefab;
    9.     public Timer newTimer = new Timer();
    10.     public int measureTime = 1;
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         newTimer.Interval = 6;
    16.         newTimer.Elapsed += new ElapsedEventHandler(TimerStart);
    17.         newTimer.Enabled = true;
    18.         newTimer.Start();
    19.  
    20.         // On spacebar press, send dog
    21.         if (Input.GetKeyDown(KeyCode.Space) & measureTime > 3)
    22.         {
    23.             Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
    24.             measureTime = 1;
    25.             newTimer.Stop();
    26.         }
    27.     }
    28.  
    29.     private void TimerStart(object o, ElapsedEventArgs e)
    30.     {
    31.         measureTime++;
    32.     }
    33. }
    34.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Don't use a Timer... that's super overkill.

    Instead, use just a float. I like to call my float "gunHeat."

    Code (csharp):
    1. private float gunHeat;
    2.  
    3. private const float TimeBetweenShots = 0.25f;  // seconds
    Now... in Update():

    Code (csharp):
    1. // cool the gun
    2. if (gunHeat > 0)
    3. {
    4.   gunHeat -= Time.deltaTime;
    5. }
    6.  
    7. // is the player asking to shoot?
    8. if (PlayerAskedToShoot)
    9. {
    10.    // can we shoot yet?
    11.    if (gunHeat <= 0)
    12.    {
    13.      // heat the gun up so we have to wait a bit before shooting again
    14.      gunHeat = TimeBetweenShots;
    15.  
    16.      // DO THE SHOT HERE
    17.    }
    18. }
    Way simpler. :)

    Now you can actually give the player powerups that reduce the TimeBetweenShots (which is a constant above, but you can make it variable).

    Or you can have SUPER shots that take a really long time to recharge, for instance.
     
    Last edited: Jan 15, 2021
  3. jtr912

    jtr912

    Joined:
    Jan 15, 2021
    Posts:
    2
    Thank you so much for the reply!
     
    Joe-Censored and Kurt-Dekker like this.
  4. BocklinIsleOfTheDead

    BocklinIsleOfTheDead

    Joined:
    Sep 21, 2021
    Posts:
    1
    yea thanks too i had the exact same problem
     
  5. miggonza01

    miggonza01

    Joined:
    Feb 11, 2023
    Posts:
    1
    Thanks!!!
     
  6. f0reversmile

    f0reversmile

    Joined:
    Aug 11, 2022
    Posts:
    1
    Дякую теж:)
     
Thread Status:
Not open for further replies.