- Home /
Fire rate for gun script, c# (not u/s)
Hello. I just started designing and testing the shooting SYSTEM first. I want my shooting to be controlled by a firerate(float) and use yield WaitForSeconds and bool 'canfire = false/true' Like this:
1st line: canfire = true; ///
2nd line: Yield WaitForSeconds blah blah ///
3rd line: canfire = true; /////////////////
So first canfire is disabled, wait seconds (firerate), then be able to fire again. Unfortunatly, C# doesn't work as simple as JavaScript. I used IEnumerator and StartCoroutine but it didn't work well. How? I had bullets--; before the yield so it made the bullet count drop to negative and all the way down and so on in a matter of seconds. Is there a way to not use coroutine? If not, can I make a different void and call it firerate and use coroutine on that? Or, how can I fix my script? Script:`using UnityEngine; using System.Collections;
public class GunScript : MonoBehaviour { public int loaded; public bool canfire; public bool reloading;
void Update ()
{
if(loaded > 0 && !reloading)
canfire = true;
if(loaded == 0)
canfire = false;
if(Input.GetMouseButton(0))
if(!reloading && canfire == true)
StartCoroutine("fire");
}
IEnumerator fire ()
{
Debug.Log("Fired!");
loaded--;
canfire = false;
yield return new WaitForSeconds(firerate);
canfire = true;
Debug.Log("Ready to fire!");
}
} `
Don't worry about the bracket errors, the insert code here is glitchy.... Also, sorry about the bad sorting at the beginning of the script, the insert code here again did that while I was editing it........... UNITY PLEASE FIX!!!
Answer by Fattie · Sep 17, 2013 at 08:14 AM
It is unbelievably simple to do this using
Invoke
and
CancelInvoke
Just read the doco.
beginners should never use yield, or touch the run loop at all, ever, for any reason.
once you master the trivial functions Invoke and CancelInvoke - which Unity put in the engine for a reason - you can learn the advanced stuff mentioned by experts on this question.
I've been looking for a better way to do timers in general within Unity. Things like enforcing fire rate and applying damage per second e.g. poison or standing in fire.
Do you have a good implementation of Invoke and CancelInvoke?
It's unbelievably simple in Unity. Invoke, InvokeRepeating etc. are all built-in to Unity. You can find literally thousands of full examples here on this site - or anywhere on the internet. Try searching on this site on "Invoke" or "InvokeRepeating"
Eg, http://answers.unity3d.com/questions/19924/how-to-call-a-routine-every-3-seconds.html just search.
Here's a whole video, made by Unity, on the topic
http://unity3d.com/learn/tutorials/modules/beginner/scripting/invoke
Try searching on the internet: "unity3d Invoke tutorial"
Your answer
Follow this Question
Related Questions
WaitForSeconds does not work 0 Answers
C# WaitForSeconds doesn't seem to work ?? 2 Answers
How to make a 3d text writeOut method? 1 Answer
Scrolling Text StartCoroutine not working C# 2 Answers
WaitForSeconds problem with Unity Pro 3 Answers