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 27, 2013 at 01:06 PM by fafase for the following reason:

The question is about timer and has been answered many times already.

avatar image
0
Question by frosstt · Aug 27, 2013 at 12:40 PM · gundelayshootray

How to put a pause in a function?

Alright, so i want the game to play out as: player shoots > has to wait 2 seconds > player can shoot again. Here is my code, I'm using raycast shooting with function updates, however i can't put a pause in there. help! #pragma strict

 var Effect : Transform;
 var TheDammage = 100;
 var Canshoot = true;
 
 
 
 
 function Update () {
 
  var hit : RaycastHit;
  var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5+Random.Range(-6,6), Screen.height*0.5+Random.Range(-6,6), 0));
  
 if (Input.GetMouseButtonDown(0) && (Canshoot === true))
 {
    if (Physics.Raycast (ray, hit, 100))
    {
           //this is where the shooting happens.
       var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                           Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                           Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                           Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
       hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);    
 
       
      
    }
 }
 }
 
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

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Jamora · Aug 27, 2013 at 12:54 PM

You just need to call a method after 2 seconds that allows you to shoot again. I would add the following lines of code:

 function AllowShooting(){
     Canshoot = true;
 }
 
 //these go inside your raycast, possibly below the SendMessage
 Invoke("AllowShooting",2);
 Canshoot = false;
 
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 Siggytron · Feb 20, 2019 at 09:19 PM 0
Share

I used this Invoke method and it worked very well for me in creating pauses between successive function calls.

avatar image
0

Answer by KiraSensei · Aug 27, 2013 at 12:54 PM

Try this :

 if (Input.GetMouseButtonDown(0) && (Canshoot === true))
 {
    if (Physics.Raycast (ray, hit, 100))
    {
           //this is where the shooting happens.
       var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                           Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                           Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                           Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
       hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);    
  
       WaitUntilNextShoot(2.0f);
  
    }
 }


And Make a new method :

 function WaitUntilNextShoot(timer:float) {
     Canshoot = false;
     yield WaitForSeconds(timer);
     Canshoot = true;
 }
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 Jamora · Aug 27, 2013 at 12:56 PM 0
Share

Beat me by a second.

avatar image KiraSensei · Aug 27, 2013 at 01:24 PM 0
Share

Close one :)

avatar image
0

Answer by beayfergm · Aug 27, 2013 at 12:58 PM

You can use a "WaitForSeconds":

     var Effect : Transform;
     var TheDammage = 100;
     var Canshoot = true;
     var timeBetweenShots = 2.0f;
 
     function Update () {
 
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5+Random.Range(-6,6), Screen.height*0.5+Random.Range(-6,6), 0));
 
         if (Input.GetMouseButtonDown(0) && (Canshoot === true))
         {
             Canshoot = false;
             WaitToShoot();
             if (Physics.Raycast (ray, hit, 100))
             {
             //this is where the shooting happens.
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
     
     function WaitToShoot() {
         // Waits "timeBetweenShots" seconds
         yield WaitForSeconds (timeBetweenShots);
         Canshoot = true;
     }
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

Follow this Question

Answers Answers and Comments

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

Related Questions

How to add a delay to my script. 3 Answers

keybind problem 0 Answers

Gun and hand animation tutorial! 2 Answers

Shooting. Bullet floats and sprays 1 Answer

FPS - Gun Delay while Rotating 0 Answers


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