- Home /
Shot Delay between button press. c#
I'm a noob, but I've looked around on the forums and stuff and even though there are solutions i just cant seem to get a grip with how they work. Maybe because the way I'm setting up my shooting method is different. Basically its like a space invaders type of game and when the player hits space bar it will instantiate a bullet(prefab), however they can just spam the button and make the game that much more easier. I want there to be a limit to how fast you can press the space bar and it to shoot. sort of like a delay maybe? I'm not sure of the exact term.
Here's the code-
using UnityEngine;
using System.Collections;
public class playerShoot : MonoBehaviour
{
public GameObject bullet;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if ((Input.GetKeyDown(KeyCode.Space)) )
{
Instantiate(bullet, transform.position, transform.rotation);
}
}
}
please can anybody help? thanks.
Answer by robertbu · Mar 16, 2014 at 04:24 PM
A simple solution is what I call a timestamp. Note there is a tiny bit of inaccuracy in the calculation but used for this, it won't be noticed by the user:
using UnityEngine;
using System.Collections;
public class playerShoot : MonoBehaviour
{
public GameObject bullet;
public float timeBetweenShots = 0.3333f; // Allow 3 shots per second
private float timestamp;
void Update ()
{
if (Time.time >= timestamp && (Input.GetKeyDown(KeyCode.Space)) )
{
Instantiate(bullet, transform.position, transform.rotation);
timestamp = Time.time + timeBetweenShots;
}
}
}
Do not forget to accept answers if they are correct. Also, sometimes, if you need to have more correct time limit, you will need to move from Update to FixedUpdate and change Time.time with Time.fixedTime.
So how would you ^ / \/ the time delay?, if it's not much trouble could you explain the 0.3333f part because i would like to know if we are able to customise the ti$$anonymous$$g, i got the shooting working, and since my game is in a demo phase i don't $$anonymous$$d but xD you know i'm interested now.
Again thanks for the code.
Answer by larry2013z · Jun 27, 2020 at 08:04 PM
//Here is another way where you only need to run Time.time when the player presses the spacebar and not every frame
public class PlayerControllerX : MonoBehaviour { public GameObject dogPrefab; public static float time; public float timeLastPass = 0.0f;
// Update is called once per frame
void Update()
{
// On spacebar press, send dog
if (Input.GetKeyDown(KeyCode.Space))
{
time = Time.time; //The time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game.
float deltaTime = time - timeLastPass; //The time since the spacebar was successfully used
if (deltaTime > 2.0f)
{
Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
timeLastPass = time;
}
}
}
}
Answer by AlejandroBoss10 · Jun 27, 2020 at 10:40 PM
I see other people have done some things but I'm going to add my 2 cents. If you have timeBetweenShots set to .25, the player has to wait .25 of a second in order to shoot again. If you have it set to 5, they have to wait 5 seconds before being able to shoot again. Hopefully this helps you out and anyone else who is looking for something like this.
public GameObject bullet;
public float timeBetweenShots;
private bool canShoot;
// Use this for initialization
void Start()
{
canShoot = true;
}
// Update is called once per frame
void Update()
{
if ((Input.GetKeyDown(KeyCode.Space) && canShoot))
{
Shoot();
}
}
void Shoot()
{
print("Shot Fired");
canShoot = false;
Instantiate(bullet, transform.position, transform.rotation);
StartCoroutine(ShotCooldown());
}
IEnumerator ShotCooldown()
{
yield return new WaitForSeconds(timeBetweenShots);
canShoot = true;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613140405im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Invoke shoot delay problem. 3 Answers
adding a delay to shooting 2 Answers
Multiple Cars not working 1 Answer
delay between shots 1 Answer
Distribute terrain in zones 3 Answers