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 superpentil · Aug 10, 2012 at 11:54 AM · instantiatefunctionbooleancoroutinesif

Questions of the Coroutines

Im messing around in C# instead of Javascript. So far its been alright to transition. But One thing I noticed was the difference in co-routines. Im trying to make a gun shoot a projectile. (this is similar to my other question about how to delay a function calling) But it looks like its better to use coroutines. So the real question is, I cant call them through the update function, but if I call them through the Start or Awake function, they dont update all the time like i need them too. How do I do this?

Heres my script:

 using UnityEngine;
 using System.Collections;
 
 public class Shooting : MonoBehaviour 
 {
     //I define the projectile here.
     public GameObject Gun;
     public Rigidbody Bullet;
     public bool canShoot = true;
     public float reloadTime = 3.2F;
     public int clipSize = 30;
     public int clipEmptyLimit = 0;
     public int clipCount = 10;
     public float fireRate = 0.2F;
     public float nextFire = 0.0F;
     public AudioClip M4A1Shot;
 
 
  // Use this for initialization
  void Start () 
     {
         canShoot = true;
  }
  
  // Update is called once per frame
  void Update() 
     {
         
         //Creating the object and applying force.
         if (Input.GetButton("Fire1") && Time.time > nextFire && clipSize >= 0 && clipCount > 0)
         {
             nextFire = Time.time + fireRate;
             Rigidbody clone;
             clone = Instantiate(Bullet, transform.position, Quaternion.identity) as Rigidbody;
             clone.velocity = transform.TransformDirection(Vector3.forward * 1024);
             audio.clip = M4A1Shot;
             audio.Play();
             clipSize -= 1;
         }
 
         if (clipSize == clipEmptyLimit)
         {
             canShoot = false;
             Gun.animation.CrossFade("Reload");
             clipSize = 30;
             clipCount -= 1;
             canShoot = true;
         }
 
         if (Input.GetKey(KeyCode.R))
         {
             canShoot = false;
             Gun.animation.CrossFade("Reload");
             clipSize = 30;
             clipCount -= 1;
             canShoot = true;
         }
 
         if (canShoot == false)
         {
 
         }
  }
 }

In Short the problem Im having is I want to wait a couple seconds after starting the reload animation so the user cant shoot until a number of seconds which will equal the length of the animation clip. Then a boolean that says they can shoot will be set back to true allowing them to shoot, and possibly have the samething work again

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

Answer by aldonaletto · Aug 10, 2012 at 12:29 PM

A simple way to solve this without using coroutines is as follows:

        ...
        // you don't need to repeat the reloading code:
        if (clipSize == clipEmptyLimit || Input.GetKey(KeyCode.R))
        {
            Gun.animation.CrossFade("Reload");
            // use nextFire to set the dead time
            nextFire = Time.time + reloadTime;
            clipSize = 30;
            clipCount -= 1;
        }
        ...
But if you really want to use a coroutine, have a boolean flag to signal when it's reloading, so that you won't start another coroutine while the previous is running:

... // you don't need to repeat the reloading code: if (clipSize == clipEmptyLimit || Input.GetKey(KeyCode.R)) { canShoot = false; // disable shots while reloading if (!reloading){ // if not already reloading... StartCoroutine(Reload()); // start Reload } } ... }

// declare the flag variable: private bool reloading = false;

IEnumerator Reload(){ reloading = true; // reload has started Gun.animation.CrossFade("Reload"); // let the animation play during reloadTime yield return new WaitForSeconds(reloadTime); clipSize = 30; clipCount -= 1; canShoot = true; reloading = false; // reload finished }

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 superpentil · Aug 13, 2012 at 11:02 PM 0
Share

It works I guess right now (im trying the way without the coroutines, but the amount of clips drop all the way down to the negative numbers so the gun is refusing to fire. Im guess this is becuase its in the update function. What can I do to make it only drop 1 clip from the clip amount when you reload from both ways (automatic and manual)? EDIT: I just found out to use Getbuttondown. Fail. Thanks.

avatar image aldonaletto · Aug 14, 2012 at 02:12 AM 0
Share

The automatic reload should be done inside the "Fire1" if, and the manual reload should respect the nextFire time:

... void Update(){ if (Input.GetButton("Fire1") && Time.time > nextFire && clipSize >= 0 && clipCount > 0){ nextFire = Time.time + fireRate; Rigidbody clone; clone = Instantiate(Bullet, transform.position, Quaternion.identity) as Rigidbody; clone.velocity = transform.TransformDirection(Vector3.forward * 1024); audio.clip = $$anonymous$$4A1Shot; audio.Play(); clipSize -= 1; // check automatic reload here: if (clipSize == clipEmptyLimit) Reload(); } // check manual reload here: if (Time.time > nextFire && Input.Get$$anonymous$$ey($$anonymous$$eyCode.R)){ Reload(); } }

 void Reload(){
     Gun.animation.CrossFade("Reload");
     // use nextFire to set the dead time
     nextFire = Time.time + reloadTime;
     clipSize = 30;
     clipCount -= 1;
 }

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

if function broken? 3 Answers

Use of boolean functions in c# 1 Answer

If function Question 1 Answer

How can I use a yeild during an udate? 1 Answer

Animation Event Bool Function? 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