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 /
This question was closed Aug 16, 2013 at 11:38 PM by DeadKenny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DeadKenny · Aug 14, 2013 at 07:55 PM · c#aishootinggundelay

Invoke shoot delay problem.

I can't figure out how to delay my gun shoots/shooting/shots.... To delay between the shots.

I'm tryong to use Invoke("Firing", shotDelay); and its not working.

For the AI I use InvokeRepeating and thats making it even worse lol.

This is the code in thee gun.

     public float shotDelay = 1.0f;
     
     
     void Firing(){
     
           Instantiate...bla bla, it works.
     
     
     }
 
 // this what is invoked by the AI which then invokes the above with delay, which is not working.
 
 void Shoot()
 
  Invoke("Firing", shotDelay);
 
 }
 }

... and this is inside the AI script

 void Shooting(){
 
     weaponRifleScript.InvokeRepeating("Shoot", 0.5f, 20);
     
     // The invoke repeatings last float 20 which is invoke rate seems to have no effect.
  
 }


Same problem with the player controlled Rifle too.

 // 
 
  void Update(){
  
    if(Input.GetKeyDown(KeyCode.Mouse01))
     {
           
       
       Invoke("Firing", shotDelay)
 
     }
 
 
    
 }


To clarify, the Invoke delay in the rifle scripts is not working. No delay is there. How do I get a good delay?

Please help me...

Thanks.

Comment
Add comment · Show 6
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 weenchehawk · Aug 14, 2013 at 08:07 PM 0
Share

Are you trying to add a delay between shots, or add a delay between someone pressing the fire button & the weapon firing ?

avatar image DeadKenny · Aug 14, 2013 at 08:19 PM 0
Share

between the shots.... will edit thread now.

avatar image weenchehawk · Aug 14, 2013 at 08:20 PM 0
Share

Adding a delay between shots can be implemented thusly:

 public class Weapon : $$anonymous$$onoBehaviour
 {
     public double RechargeTime = 0;
     private double NextFireTime = 0;
 
     public Awake()
     {
         NextFireTime = Time.time;
     }
 
     public bool HasWeaponRecharged()
     {
         return (Time.time > NextFireTime);
     }

     public void Update()
     {
       if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)&& HasWeaponRecharged())
          FireWeapon();
     }

     public void FireWeapon()
     {
       // Instantiate......
 
        endDelay = Time.time + shotDelay;
     }  

 }
avatar image DeadKenny · Aug 14, 2013 at 09:07 PM 0
Share

Now the bullet is not co$$anonymous$$g out?

 // endDelay here is NextFireTime.
 
 public double shotDelay = 10;
 public double endDelay = 0;
 
 // is it supposed to be onlu public Awake? because it gives error unless I put public void.
 public void Awake(){
 
    endDelay = Time.time;
 
     
 }
 
 public bool Recharged(){
 
 
      return (Time.time > endDelay);
 
 
 }
 
 public void Shoot()
 
   if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)&& Recharged())
 
 {
    Instantiate......
 
   endDelay = Time.time + shotDelay;
 }
 
avatar image DeadKenny · Aug 14, 2013 at 09:13 PM 0
Share

It works if I put public void Shoot as Update but then the delay does not work again.

Show more comments

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Sajidfarooq · Aug 14, 2013 at 09:37 PM

I've modified Weenchehawk's code slightly. The following code is tested, and works.

 public double shotDelay = 10;
 private double endDelay = 0;
     
 private int shotNumber = 0;
  
 // Yes, it is supposed to be public
 public void Awake()
 {
     endDelay = Time.time;
 }
  
 public bool Recharged()
 { 
     return (Time.time > endDelay);
 }
 
 public void Shoot()
 {
  
     if(Input.GetKeyDown(KeyCode.Mouse0)&& Recharged())
     {
         Debug.Log ("Shooting bullet " + shotNumber.ToString()); 
         endDelay = Time.time + shotDelay;
         shotNumber++;
     }
 }
     
 public void Update()
 {
     Shoot();
 }
Comment
Add comment · Show 4 · 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 weenchehawk · Aug 14, 2013 at 09:47 PM 1
Share

Something like that should do it, Deadkenny, if it works you might like to credit SajidFarooq / myself with an answer.

avatar image DeadKenny · Aug 16, 2013 at 09:40 PM 0
Share

Ok both work.

Thanks dudes.

avatar image DeadKenny · Aug 16, 2013 at 09:49 PM 0
Share

Last question though, which code is more efficient, Igors or yours?

avatar image Sajidfarooq · Aug 17, 2013 at 04:01 AM 0
Share

There is no difference in efficiency. $$anonymous$$ine is more readable which is good in the long run when you want to modify your code later. Igor's more compact.

$$anonymous$$indly do mark this question as answered when you decide the answer you prefer. Closing the thread, and thumbs-up doesn't automatically mark the question as answered.

avatar image
2

Answer by IgorAherne · Aug 14, 2013 at 08:15 PM

Example for player controlled Rifle:

 float currTime = 0;
 
 
     void Update(){
      
        if(Input.GetKeyDown(KeyCode.Mouse01) && currTime + shootDelay < Time.time)
         {
           Invoke("Firing", shotDelay);
           currTime = Time.time;
         }

Please check my thread, I will be very thankful :) http://forum.unity3d.com/threads/191923-Full-corse-how-to-make-a-horror-game-AVAILABLE

Comment
Add comment · Show 4 · 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 DeadKenny · Aug 14, 2013 at 08:23 PM 0
Share

Cool I will check that thread.

avatar image DeadKenny · Aug 14, 2013 at 08:30 PM 0
Share

It does not work guy.

avatar image IgorAherne · Aug 15, 2013 at 08:00 AM 0
Share

yeah, I've forgot 1 more line in Update. Should be working now

avatar image DeadKenny · Aug 16, 2013 at 09:43 PM 0
Share

Thanks guy.

avatar image
0

Answer by weenchehawk · Aug 16, 2013 at 10:05 PM

You know, it's funny. I answered this question in another thread on a very similar topic just last night (here : link text). The short version is that most compilers will optimize the sh1t out of most of your statements way better than you ever can. Human's are better at optimizing algorithems though.

I daresay the compiled code in both is probably equally efficient but even if it weren't there wouldn't be much in it. Go with whatever is most readable / maintainable for yourself.

Comment
Add comment · Show 1 · 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 DeadKenny · Aug 16, 2013 at 11:38 PM 0
Share

Cool. Ok well this worked out.

You guys answer pretty quick. Glad I got this engine.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Reload Ammo is not working 0 Answers

Reload Ammo is not working 1 Answer

Multiple Cars not working 1 Answer

adding a delay to shooting 2 Answers

[I REALLY NEED HELP FAST]Help with enemy Shooting 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