Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jul 13, 2018 at 09:44 PM by MilkyPancack for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MilkyPancack · Jul 13, 2018 at 07:37 PM · ienumeratorpublicvoidprivateiterate

How do I combine a void with an iteration?

Trying to make an fps and i need to combine an Iteration with a void:

using UnityEngine; using System.Collections;

public class Gun : MonoBehaviour {

 public float damage = 10f;
 public float range = 100f;
 public float fireRate = 15f;

 public int maxAmmo = 30;
 public int currentAmmo;
 public int bulletsLeft = 200;
 public float reloadTime = 1f;
 public AudioClip reload1;
 private bool isReloading = false;
 AudioSource audio;
 private bool playReloadSound = false;



 public Camera fpsCam;
 public ParticleSystem muzzleFlash;
 public GameObject impactEffect;

 private float nextTimeToFire = 0f;

 public Animator animator;

 void Start ()
 {
     currentAmmo = maxAmmo;
     audio = GetComponent<AudioSource>();
     animator.SetBool("Fire", false);
 }

 
 void Update () {
     if (isReloading)
         return;


     if (playReloadSound)
         audio.PlayOneShot(reload1);
         playReloadSound = false;


     if (currentAmmo <= 0)
     {
         StartCoroutine(Reload());
         return;
     }

     if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
     {
         nextTimeToFire = Time.time + 1f / fireRate;
         StartCoroutine(Shoot());
         AudioSource shoot = GetComponent<AudioSource>();
         shoot.Play();

     }

     if (bulletsLeft <= 0) return;



 }

 IEnumerator Reload () //combine this
 {
     isReloading = true;
     Debug.Log("Reloading...");

     animator.SetBool("Reloading", true);

     playReloadSound = true;

     yield return new WaitForSeconds(reloadTime);

     animator.SetBool("Reloading", false);

     currentAmmo = maxAmmo;

     isReloading = false;
 }

 private void Reload2() //with this
 {
     if (bulletsLeft <= 0) return;

     int bulletsToLoad = maxAmmo - currentAmmo;
     int bulletsToDeduct = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft;

     bulletsLeft -= bulletsToDeduct;
     currentAmmo += bulletsToDeduct;
 }

 IEnumerator Shoot()
 {
     muzzleFlash.Play();
     animator.SetBool("Fire", true);
     currentAmmo--;
     yield return new WaitForSeconds(0.05f);
     animator.SetBool("Fire", false);


     RaycastHit hit;
     if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
     {
         Debug.Log(hit.transform.name);

         Target target = hit.transform.GetComponent<Target> ();
         if (target != null) 
         {
             target.TakeDamage (damage);
         }

         GameObject impactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
         Destroy(impactGo, 2f);
     }
 }
     

}

Comment
Add comment · Show 2
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 JVene · Jul 13, 2018 at 08:41 PM 0
Share

Read your post from our perspective and notice we can't tell where the Iteration or the void you're asking about is located in your code.

avatar image MilkyPancack JVene · Jul 13, 2018 at 09:43 PM 0
Share

I added a // comment next to each

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by josiahsrc · Jul 13, 2018 at 07:56 PM

Nice job with the coroutine. I believe solves the combining issue, but you may want to check if you have any bullets left before you call reload. Cheers

 IEnumerator Reload()
     {
         isReloading = true;
 
         // Reload process
         Debug.Log("Reloading...");
         animator.SetBool("Reloading", true);
         playReloadSound = true;
         yield return new WaitForSeconds(reloadTime);
         animator.SetBool("Reloading", false);
 
         // Finished reloading
         int bulletsToLoad = maxAmmo - currentAmmo;
         if (bulletsToLoad > bulletsLeft)
             bulletsToLoad = bulletsLeft; // Only load the amount of bullets you have left
         bulletsLeft -= bulletsToLoad; // What you load is what you lose
         currentAmmo += bulletsToLoad; 
 
         isReloading = 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

Follow this Question

Answers Answers and Comments

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

Related Questions

Slow down player (2d) when in contact with object 0 Answers

Coroutine design problem? 3 Answers

Using [SerializeField] vs public 2 Answers

Field is never assigned to and will always have its default value. 0 Answers

Difference between [HideInInspector] public variable and using private variable with getter/setter property 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