Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dreave · Dec 04, 2011 at 01:05 PM · animationshootingreload

Wait for reload to finish?

I have a script which covers most of my weapons shooting and reloading, but I have this 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 this script so that you have to wait a few seconds before you can start shooting again?

this 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 hit : RaycastHit;

var range = 500;

function Shoot (){

 if(amountOfShots > 0){

     amountOfShots--;

     if(shootSound){

     audio.PlayOneShot(shootSound);

 }

if(Physics.Raycast(transform.position, transform.forward, hit, range)){

 var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);



 if(hit.collider.tag == "Enemy"){

     if(bloodPrefab){

         Instantiate(bloodPrefab, hit.point, rot);

     }

         hit.collider.gameObject.SendMessage("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver);

     }else{

     if(sparksPrefab){

         Instantiate(sparksPrefab, hit.point, rot);

         }

     }

 }

}

}

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
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 this. Now wonder it does not work.

Good luck.

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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Reloading Help 2 Answers

Reload Tutorial? 0 Answers

Blender animation 1 Answer

adding animation dynamically by script 0 Answers

Animation Script Help 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