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 /
avatar image
0
Question by FredVicentin · Oct 11, 2011 at 03:04 PM · yielddelayreload

How to reload a gun with delay ?

Hey guys, I want to generate a delay to reload the weapon. For exemple, when the player press "r" the gun reload but have a delay time, I tryied to make it but its gone wrong, i used yield, but it don't work. Can someone help me ? Don't need be with yield.

Here my script:

 using System.Linq;

using System.Text; using UnityEngine;

 public class Laucher : MonoBehaviour
 {
     public Rigidbody projectile;
     public float reloadDelay = 10;
     public float speed = 20;
     public int bullet = 6;
     public int reload = 40;
     public float reloadTime;
     private bool canshoot = true;
     private bool recarrega = false;
     private int reloadnum;
     private GUIText balas ;
     private GUIText cargas;

     

     



     public void Update()
     {
         Debug.Log(reload);
         if (canshoot)
         {
             reloadnum = 6 - bullet;                    
                             
             if (Input.GetButtonDown("Fire1"))
             {
                 Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
                 instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
                 Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
                 if (instantiatedProjectile)
                 {
                     bullet--;
                     if (bullet <= 0)
                     {
                         bullet = 0;
                         canshoot = false;
                         reloadnum = 6;
                         
                     }
                     
                    
                 }

             }
         }

         if (Input.GetKeyDown("r") && reload > 0)
         {
             reload -= reloadnum;
             bullet += reloadnum;
             canshoot = true;
             recarrega = false;
         }
             if (reloadnum < 0)
             {
                 reloadnum = 0;
             }
         else if (Input.GetKeyDown("r") && reload < 6 && reload > 0)
         {
             reloadnum = reload - bullet;
         }
     }

    /*public IEnumerator recarregar()
     {
     if (recarrega == true)
 
     {
         yield return new WaitForSeconds(5);
         reload -= reloadnum;
         bullet += reloadnum;
         canshoot = true;
         recarrega = false;
            
     }
      */

 }
Comment
Add comment · Show 3
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 timsk · Oct 11, 2011 at 03:28 PM 0
Share

I can't answer your question, but to help others. Can you clarify what you mean by "but it wont work" and "gone wrong".

Just try to explain exactly what isn't working, what were you expecting to happen exactly and what is happening ins$$anonymous$$d. If this isn't the case, state any errors you are getting.

avatar image Arbiter · Oct 11, 2011 at 04:37 PM 0
Share

You cannot use Yield the way you do. You cannot yield in Void Update Use a custom function. Something like so:

if(Input.Get$$anonymous$$eyDown("r")){ Reload(); }

Void Reload(){ yield WaitForSeconds(3); //Continue here. }

avatar image timsk · Oct 11, 2011 at 04:56 PM 0
Share

Ah... it makes sense now, i shouldve been able to help :D.

if you use yield in Update(). Your telling unity to wait every frame ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Arbiter · Oct 11, 2011 at 04:48 PM

Use a different function to reload. You cannot use yield WaitForSeconds in Void Update. Do something like this:

 public int reloadTime = 2.5;
 
 if(Input.GetKeyDown("r")){
     Reload(); //Call the reload function defined below
 }
 
 function Reload(){
 yield WaitForSeconds(reloadTime);
 //Do whatever you want.
 }

You may also want to take a look at this.

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

Answer by cmccown · Oct 11, 2011 at 03:54 PM

Don't use yield, that will force your program to wait.

Instead use a timer of some sort.

One solution would be to create a pair of new variables:

private bool reloading = false; // Is the player reloading?

private float reloadTimer = 10; // Reload Timer

Then when the player presses the reload button set the reloading value to true.

In your update event if the player is reloading, then you can call a function that will subtract from reload timer using delaTime until the timer expires then test if reload timer is less than 0.

If the reload timer reaches 0 or below, you can reload the players weapon, reset the timer to the reload delay and set the reloading property to false.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Reloading delay problem.. 0 Answers

i need to delay an action but my code isnt working 1 Answer

How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers

Wait to Reload 2 Answers

How to change a Texture with a Delay 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