- Home /
How to add a delay to my script.
Okay so recently i asked about my gun that's bugging out and i got one awesner and it said:
Just glancing over it, it looks like:
yield WaitForSeconds(FireRate);
inside your fire function could be giving you some trouble! It doesnt count down ammo before after the fire rate. But I would try putting the delay somewhere else, so that you can't shoot another bullet until the firerate is done.
Here is my code:
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag : float = 30;
var IsFullAuto = true;
static var AmmoLeft : float;
static var ReloadTTime : float;
private var CanFire = true;
var FireRate = 0.1;
static var IsReloading = false;
function Start () {
AmmoLeft = AmmoInMag;
ReloadTTime = ReloadTime;
}
function Update () {
if(IsFullAuto == false) {
if(Input.GetButtonDown("Fire1")){
if(AmmoLeft > 0) {
BroadcastMessage("FireAnim");
Fire();
}
}
}
else{
if(Input.GetButton("Fire1")){
if(AmmoLeft > 0) {
BroadcastMessage("FireAnim");
Fire();
}
}
}
if(AmmoLeft == 0)
{
Reload();
}
if(AmmoLeft < 0){
AmmoLeft = 0;
}
}
function Fire () {
if(CanFire == true && IsReloading == false){
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(transform.forward*BulletSpeed);
CanFire = false;
yield WaitForSeconds(FireRate);
CanFire = true;
AmmoLeft -= 1;
audio.Play();
}
}
function Reload () {
CanFire = false;
IsReloading = true;
BroadcastMessage("ReloadAnim");
yield WaitForSeconds(ReloadTime);
IsReloading = false;
CanFire = true;
AmmoLeft = AmmoInMag;
}
How do I add a delay?
Thank you.
PS here is a link to the problem post: http://answers.unity3d.com/questions/382963/when-reloading-my-gun-sounds-and-the-bullets-glitc.html
PPS here is a link to a YouTube video showing the problem: http://www.youtube.com/watch?v=Zt4le1G0iYo&feature=youtu.be
Just a question, why do you have the audio play after you can shoot again? This goes as well for the ammo dropping. I just can't see why you have that there. :p
What do you mean have the audio play? If you think i should turn it off or what ever how? Im a complete noob
Well, what I mean is that its like say, you have a plasma cannon (random example, it had to be something that doesn't shoots bullets. If you were using "real" bullets, you would be better of with Physics.raycast), you would fire this plasma cannon, see the shot go, but hear nothing. Then, when you can fire once again, you hear the blast. $$anonymous$$ind of weird if you ask me.
I would put it right before your can fire. That would be more realistic.
I don't have the time to answer your question fully. If it's still unanswered by the time I can, I will try to help. :)
Ahh i see i might put that in :) and thanks i hope you have time.
Answer by Meater6 · Jan 19, 2013 at 02:26 PM
Ok, well now I have the time. Saw your vid too. Very impressive for a "complete noob" ;). Well, just so you know, your fire rate is quite fast. Not saying that may be the only problem (or even one), but its possible.If it shoots once per frame, then it usually shoots faster than that... Depending on your frame rate.
Well, try this
//Set this to Time.time when you fire.
var fireTimerStart : float;
function Update () {
//other stuff....
if(Time.time >= fireTimerStart + fireRate) {
canFire = true;
}
}
If you set fireTimerStart right after you shoot, it should work, in theory. I say that because it wasn't tested. Just wrote it on the fly :P. Hope it works though.
Also some general notes about scripting:
Usually you declare variable with the first letter lowercase. This is called camel hump I think... eg. bulletSpeed. This makes it look nicer in unity.
When using if statements with booleans, you can just say
if(canfire)
, instead ofif(canfire == true)
. For false, you can doif(!canfire)
. This is very useful to check to see if something is null. Like say a variable "thingamajig" is of type GameObject and it is not assigned, you can check that by sayingif(thingamajig)
orif(!thingamajig)
very useful to know.
I hope the timer is what you are looking for. But before you try it, check to see what happens when you increase the fire rate, to say 2.
I hope you find the answer! :D
EDIT:
I had to reverse engineer your script that you posted in the comments.
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag : float = 30;
var IsFullAuto = true;
static var AmmoLeft : float;
static var ReloadTTime : float;
private var CanFire = true;
var FireRate = 0.1;
static var IsReloading = false;
var fireTimerStart : float;
function Start ()
{
AmmoLeft = AmmoInMag; ReloadTTime = ReloadTime;
}
function Update ()
{
if(Time.time >= fireTimerStart + FireRate)
{
canFire = true;
}
if(IsFullAuto == false)
{
if(Input.GetButtonDown("Fire1"))
{
if(AmmoLeft > 0)
{
BroadcastMessage("FireAnim");
Fire();
}
}
}
} //Meater6: You were missing this close bracket!
//(i have cut the bottom out) And setting my FireRate to 2 dosnt help D: and what would i set the fireTimeStart to if my FireRate is at 0.1?
Ok, I think I see your problem. You never set fireStartTime. Also, you should be sure that canFire is false while the timmer is going. Set it to Time.time right after you fire, this means preferably after you instantiate the bullet or fire the raycast. But this should work too (Find this section in your script, and the line):
if(AmmoLeft > 0)
{
BroadcastMessage("FireAnim");
Fire();
//NEW LINES BELOW
fireStartTime = Time.time;
canFire = false;
}
And that's it. I mean, in theory it should work.
Hey thanks but I haven't fixed it (I don't know if the script doesn't work or I'm not doing it right)
Here is how it looks when i put it into my script:
var Bullet : Rigidbody; var Spawn : Transform; var BulletSpeed : float = 1000; var ReloadTime : float = 2; var AmmoIn$$anonymous$$ag : float = 30; var IsFullAuto = true; static var AmmoLeft : float; static var ReloadTTime : float; private var CanFire = true; var FireRate = 0.1; static var IsReloading = false; var fireTimerStart : float;
function Start () { AmmoLeft = AmmoIn$$anonymous$$ag; ReloadTTime = ReloadTime;
} function Update () { if(Time.time >= fireTimerStart + FireRate) { canFire = true; } if(IsFullAuto == false) { if(Input.GetButtonDown("Fire1")){ if(AmmoLeft > 0) { Broadcast$$anonymous$$essage("FireAnim"); Fire(); } } }
(i have cut the bottom out) And setting my FireRate to 2 dosnt help D: and what would i set the fireTimeStart to if my FireRate is at 0.1?
Thnaks
Hey, do yo $$anonymous$$d posting that in a edit to your original question? That way you can format it. I'm only human, and its difficult to read, but I'll do what I can. I'll make an edit to my answer.
I'm glad it works for you! You know... there is one more way to thank other users... up-voting. It gives the user +15 karma. You should give an up-vote whenever you find something useful. Though I'm not sure if you can up-vote when your less than 10 karma :)
Don't feel obligated to up-vote my answer!
Answer by fafase · Jan 23, 2013 at 08:09 PM
What you need is a timer that you reset. From a quick look all other answers do not reset the timer meaning it waits and then trigger 60 times per second. I could be wrong though I just overlooked...
var timer :float = 0;
var fireRate = 0.5f;
function Update () {
if(Input.GetMouseButton(0)){
timer -=Time.DeltaTime;
if(timer <= 0) {
Fire();
timer = fireRate;
}
}
if(Input.GetMouseButtonUp(0))timer=0.0f;
}
When you press, timer is 0 so Fire is called and timer is set to the fireRate. As you keep pressing timer is decreased but as long as it is not 0 Fire is not called. That is until obviously timer reaches 0.
Also, when you release the button, you want to be able to shoot right away next time you press as it would not make sense to have a delay. So GetMouseButtonUp set timer back to 0.
One side note, I see you use AddForce for your projectile. You should have a look at frame miss principle (I think that is how it is called...).
As the projectile goes real fast, let's say 200px/sec it means at frame 0 it is at 0, frame 1 at 200, frame 2 at 400 and so on but never in between (that is how computer works). Now what if you have a wall of 50px width at 100 (so from 100 to 150)? I give you a clue, the bullet "gets through" and if you think you are safe behind, well guess what, you're not.
This is a common case. You can slightly fix it using continous collision but the cost on performance is not worth. Use raycast to check collision and keep the rigidbody for visual effect only.
Answer by TanveerSB · Jan 23, 2013 at 11:51 AM
javascript Version:
Call MyWaitFunc() where you want to delay.
function MyWaitFunc () {
yield WaitForSeconds (5);
print("This line will print after 2 seconds");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613091211im_/https://answers.unity.com/themes/thub/images/avi.jpg)