Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by blobbs30 · Jan 19, 2013 at 10:39 AM · javascriptgundelayshoot

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meater6 · Jan 19, 2013 at 10:48 AM 0
Share

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

avatar image blobbs30 · Jan 19, 2013 at 11:00 AM 0
Share

What do you mean have the audio play? If you think i should turn it off or what ever how? Im a complete noob

avatar image Meater6 · Jan 19, 2013 at 12:03 PM 0
Share

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. :)

avatar image blobbs30 · Jan 19, 2013 at 12:36 PM 0
Share

Ahh i see i might put that in :) and thanks i hope you have time.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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 of if(canfire == true). For false, you can do if(!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 saying if(thingamajig) or if(!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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blobbs30 · Jan 22, 2013 at 09:26 AM 0
Share

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

avatar image Meater6 · Jan 22, 2013 at 04:02 PM 0
Share

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.

avatar image blobbs30 · Jan 23, 2013 at 08:28 AM 0
Share

okay Ill have a look at it later thanks :)

avatar image blobbs30 · Jan 23, 2013 at 08:38 PM 2
Share

Thank you so so much! Its fixed i cant thank you enough

avatar image Meater6 · Jan 24, 2013 at 06:35 AM 0
Share

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!

Show more comments
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jan 23, 2013 at 08:16 PM 0
Share

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.

avatar image blobbs30 · Jan 23, 2013 at 08:39 PM 0
Share

Thank you' but i had just finished when you posted :/

avatar image
1

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");
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to put a pause in a function? 3 Answers

Bullets will not fire forwards 1 Answer

Animation plays at original position? 1 Answer

I want to be able to make a good shoot script that actually works. 0 Answers

How do I modify my script to give my gun recoil when I shoot? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges