• 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
Question by Dreave · Dec 04, 2011 at 01:05 PM · animationshootingreload

Wait for reload to finish?

I have a script w$$anonymous$$ch covers most of my weapons shooting and reloading, but I have t$$anonymous$$s problem when you press r you dont have to wait for the reload animation to finish instead you can press r and start shooting straight away. How would I alter t$$anonymous$$s script so that you have to wait a few seconds before you can start shooting again?

t$$anonymous$$s is the script,

 var amountOfShots = 8;



function Update (){

if(Input.GetButtonDown("Fire1")){

 Shoot();

 }

if(Input.GetKeyDown("r")){

 amountOfShots = 8;

 }

}

var shootSound : AudioClip;

var bloodPrefab : Transform;

var sparksPrefab : Transform;

var $$anonymous$$t : RaycastHit;

var range = 500;

function Shoot (){

 if(amountOfShots > 0){

     amountOfShots--;

     if(shootSound){

     audio.PlayOneShot(shootSound);

 }

if(Physics.Raycast(transform.position, transform.forward, $$anonymous$$t, range)){

 var rot = Quaternion.FromToRotation(Vector3.up, $$anonymous$$t.normal);



 if($$anonymous$$t.collider.tag == "Enemy"){

     if(bloodPrefab){

         Instantiate(bloodPrefab, $$anonymous$$t.point, rot);

     }

         $$anonymous$$t.collider.gameObject.SendMessage("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver);

     }else{

     if(sparksPrefab){

         Instantiate(sparksPrefab, $$anonymous$$t.point, rot);

         }

     }

 }

}

}

Comment

People who like this

0 Show 0
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
Best Answer

Answer by FLASHDENMARK · Dec 04, 2011 at 01:09 PM

 var reloadTime = 2.5;
 yield WaitForSeconds(reloadTime); // Waits 

You should really search before you ask these questions they have already been answered.

http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html

EDIT: Update cannot be coroutined you will have to do your reload stuff in a new function:

 var reloadTime = 2.5;
 
 function Update (){
 if(Input.GetKeyDown("r")){
     Reload(); //Calls the reload function 
     }
 }
 
 function Reload (){
 yield WaitForSeconds(reloadTime);
 amountOfShots = 8;
 }

Now it should work. I am really sorry that I forgot to tell you about t$$anonymous$$s. Now wonder it does not work.

Good luck.

Comment
LegionIsTaken

People who like this

1 Show 5 · 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 Dreave · Dec 04, 2011 at 01:53 PM 0
Share

sorry about that, where would I put it in the script?

avatar image FLASHDENMARK · Dec 04, 2011 at 03:33 PM 0
Share

Come on, this is not really about scripting anymore, now it is more of a logical issue. Brake the problems into simple and logical portions. You answered your question yourself by saying: "How would I alter this script so that you have to wait a few seconds before you can shoot again.

Try thinking a little about the problem. Trail and error is also one of the best ways of learning; you try something, it doesn't work, you try again, repeat, profit.

Good luck.

avatar image Dreave · Dec 04, 2011 at 04:04 PM 0
Share

Im sorry for being a pain ive taken your advise and tried over and over again but im just terrible at scripting, I know this way would be better for me to learn but I cant figure it out it either dosent work or I get an error saying "Update() can not be a coroutine" please can you help me?

avatar image FLASHDENMARK · Dec 04, 2011 at 04:36 PM 0
Share

Oh, damn I totally forgot to tell you that sorry.

In Update()(Including other function) you cannot use yield WaitForSeconds you will need to define a new function for that to work. I will update my post.

avatar image Dreave · Dec 04, 2011 at 05:58 PM 0
Share

Thanks for all your help you've been very helpful! thanks again!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Reload Tutorial? 0 Answers

Reloading Help 2 Answers

Trying to simply play this animation... Help please. 1 Answer

Reacting to Mouse animation... 2 Answers

Loop animation on button hold 2 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