Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by MC HALO · Jul 24, 2011 at 04:19 AM · fpsshoot

How Can i add a delay to my shoot script ?

Hi all i just made a shooting script and it works perfectly :) but the problem is the shooting is to fast i want a delay in there so if i press F on the keyboard rapidly it wont let me shoot until the delay time is over. I want to set the delay time as 1.5 sec and then the you can shoot again. After 1.5 sec you can shoot :) when you press F on the keyboard

Here is my script:

         var Range : float;
         
                 var Force : float = 1000;
 
                     var Clips : float = 100;
                     
                         var BulletsPerClip : float = 1;
                         
                         var ReloadTime: float = 0.6;
                         
                         
  var BulletsLeft : float = 0;
 
  
  var Delay : float = 2;
 var fireRate = 0.05;
 
 public var ShootSound : AudioClip;
 public var ReloadSound : AudioClip;
 function Update () {
 
         if(Input.GetKeyDown(KeyCode.F) && BulletsLeft ){
 
          PlayShootSound();
           ShootRay();
            
             
 
 }
 
 }
 
 function Start(){
 
 
    BulletsLeft = BulletsPerClip;
 
 
 
 }
 
 
 
 
 function ShootRay(){
 
   GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Shoot") ;
     var hit: RaycastHit;
     
     var direction = transform.TransformDirection(Vector3.forward);
     
     Debug.DrawRay(transform.position, direction * Range, Color.yellow);
             
     if(Physics.Raycast( transform.position, direction, hit, Range)){
     
         if(hit.rigidbody){
         
     
          hit.rigidbody.AddForceAtPosition( Force * direction , hit.point);
        
     }
     }
 
 BulletsLeft--;
 
 if( BulletsLeft < 0){
   
      BulletsLeft = 0;
  }    
 
 if( BulletsLeft == 0){
  
  Reload();
 
 }
 }
 
 
 function Reload (){
 
 PlayReloadSound();
 GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Reload") ;
  yield WaitForSeconds( ReloadTime);
    
    if( Clips >0){
  
   Clips -=1;
   BulletsLeft = BulletsPerClip; 
   
  }
 }
 
     function PlayShootSound()
     {
         audio.PlayOneShot(ShootSound);
     }
 
 function PlayReloadSound()
     {
         audio.PlayOneShot(ReloadSound);
     }

Please someone help me thank you so much :) MCHALO

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by testure · Jul 24, 2011 at 04:26 AM

use invoke to delay calling something. If you want to keep people from being able to mash on buttons as fast as they can, you can just do a conditional time test. IE)

if(Time.time - startTime >= minShotTime)    
    ShootGun();
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 MC HALO · Jul 24, 2011 at 04:45 AM 0
Share

It does not take no effect the bullets still go in the same speed and there is no delay

avatar image MC HALO · Jul 24, 2011 at 04:46 AM 0
Share

Can you please use my variable name in the condition you have written :). Do i place it in the update function ?

avatar image testure · Jul 24, 2011 at 04:48 AM 0
Share

Sorry, maybe someone else will- but I have a strict 'no writing code for people' policy. I don't $$anonymous$$d giving an example and explaining something if it's not clear, but I'm not going to write your code for you.

avatar image MC HALO · Jul 24, 2011 at 04:54 AM 0
Share

dont worry about it mate im sure someone will

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast hitting below mouse position :( 0 Answers

Question about Gun Sounds 1 Answer

Fps weapon shoot/monster damage 2 Answers

Destroy gameObject on collision/time? 0 Answers

Problem with firing laser in direction of camera 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