• 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 /
  • Help Room /
avatar image
0
Question by Wesley21spelde · Jan 22, 2021 at 06:35 PM · programmingnot workingcounterammoleft

Bullet Counter Not Working Propperly

hey guys i have 2 scripts that handle my weapon i have a problem and dont exactly know how to fix it the problem is in my player_shooting.... Script.... my ammo left is goint faster than my bulletrs that i fire but when i set it to getbuttonDown (Single Shot ).. its fine but t$$anonymous$$s is for a fast firing gun can anyone help me fix t$$anonymous$$s here are the scripts.... Thanks! -------------------//

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Player_Shooting_Weapon : MonoBehaviour
 {
 
     
     float cooldown = 0;
 
     public GameObject muzzleFlash;
     FX_Manager fxManager;
     WeaponData weaponData;
     Animator anim;
 
     public int maxAmmo = 0;
     public int AmmoLeft = 10;
     private int currentAmmo;
     public float reloadTime = 1f;
     private bool isReloading = false;
 
     public AudioSource audioSource;
     public AudioClip ReloadSoundFx;
 
 
     public Text AmmoLeftText;
 
     private void Start()
     {
         if (currentAmmo == -1f)
 
             currentAmmo = (int)maxAmmo;
 
 
         fxManager = GameObject.FindObjectOfType<FX_Manager>();
 
         if(fxManager == null)
         {
             Debug.Log("Couldn`t find an FX_Manager");
         }
     }
 
     IEnumerator Reload()
     {
         isReloading = true;
         muzzleFlash.SetActive(false);
         audioSource.PlayOneShot(ReloadSoundFx, 0.7f);
         // Anima.SetBool("Reloading",true)
         Debug.Log("Reloading....");
         yield return new WaitForSeconds(reloadTime - .25f);
 
         // Anima.SetBool("Reloading",false)
         yield return new WaitForSeconds(.25f);
         currentAmmo = (int)maxAmmo;
         isReloading = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         
 
         if (AmmoLeftText != null)
         {
             AmmoLeftText.text = AmmoLeft.ToString();
         }
 
         if (isReloading)
             return;
 
         if (AmmoLeft <= 0)
         {
             muzzleFlash.SetActive(false);
             return;
         }
 
         if (currentAmmo <= 0)
         {
             StartCoroutine(Reload());
             return;
         }
 
         if (Input.GetMouseButton(0))
         {
             // Player wants to shoot...so. Shoot.
             Fire();
             
             
         }
         if (Input.GetMouseButtonUp(0))
         {
             muzzleFlash.SetActive(false);
 
 
         }
         cooldown -= Time.deltaTime;
     }
     void OnEnable()
     {
         isReloading = false;
         // Anima.SetBool("Reloading",false)
     }
     void Fire()
     {
         AmmoLeft--;
         currentAmmo--;
 
         if (weaponData == null)
         {
             weaponData = gameObject.GetComponentInC$$anonymous$$ldren<WeaponData>();
             if (weaponData == null)
             {
                 Debug.Log("Did not find any WeaponData in our c$$anonymous$$ldren!");
                 return;
             }
         }
 
         //muzzleFlash.SetActive(true);
         if (cooldown > 0)
         {
             return;
         }
 
         muzzleFlash.SetActive(true);
         Debug.Log("Firing our gun!");
 
         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
         Transform $$anonymous$$tTransform;
         Vector3 $$anonymous$$tPoint;
 
         $$anonymous$$tTransform = FindClosestHitObject(ray, out $$anonymous$$tPoint);
 
         if ($$anonymous$$tTransform != null)
         {
             Debug.Log("We $$anonymous$$t: " + $$anonymous$$tTransform.name);
 
             // We could do a special effect at the $$anonymous$$t location
             // DoRicochetEffectAt( $$anonymous$$tPoint );
 
             Health h = $$anonymous$$tTransform.GetComponent<Health>();
 
             w$$anonymous$$le (h == null && $$anonymous$$tTransform.parent)
             {
                 $$anonymous$$tTransform = $$anonymous$$tTransform.parent;
                 h = $$anonymous$$tTransform.GetComponent<Health>();
             }
 
             // Once we reach here, $$anonymous$$tTransform may not be the $$anonymous$$tTransform we started with!
 
             if (h != null)
             {
                 // T$$anonymous$$s next line is the equivalent of calling:
                 //                    h.TakeDamage( damage );
                 // Except more "networky"
                 PhotonView pv = h.GetComponent<PhotonView>();
                 if (pv == null)
                 {
                     Debug.LogError("Freak out!");
                 }
                 else
                 {
                     h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllViaServer, weaponData.damage);
                 }
 
             }
 
             if (fxManager != null)
             {
                 muzzleFlash.SetActive(true);
                 
                 DoGunFX($$anonymous$$tPoint);
             }
         }
         else
         {
             // We didn't $$anonymous$$t anyt$$anonymous$$ng (except empty space), but let's do a visual FX anyway
             if (fxManager != null)
             {
                 muzzleFlash.SetActive(true);
                 $$anonymous$$tPoint = Camera.main.transform.position + (Camera.main.transform.forward * 100f);
 
                 DoGunFX($$anonymous$$tPoint);
 
             }
 
         }
         
         cooldown = weaponData.fireRate;
         
     }
 
 
     void DoGunFX(Vector3 $$anonymous$$tPoint)
     {
         
         WeaponData weaponData = gameObject.GetComponentInC$$anonymous$$ldren<WeaponData>();
 
         fxManager.GetComponent<PhotonView>().RPC("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, $$anonymous$$tPoint);
 
     }
 
     Transform FindClosestHitObject(Ray ray, out Vector3 $$anonymous$$tPoint)
     {
 
         RaycastHit[] $$anonymous$$ts = Physics.RaycastAll(ray);
 
         Transform closestHit = null;
         float distance = 0;
         $$anonymous$$tPoint = Vector3.zero;
 
         foreach (RaycastHit $$anonymous$$t in $$anonymous$$ts)
         {
             if ($$anonymous$$t.transform != t$$anonymous$$s.transform && (closestHit == null || $$anonymous$$t.distance < distance))
             {
                 // We have $$anonymous$$t somet$$anonymous$$ng that is:
                 // a) not us
                 // b) the first t$$anonymous$$ng we $$anonymous$$t (that is not us)
                 // c) or, if not b, is at least closer than the previous closest t$$anonymous$$ng
 
                 closestHit = $$anonymous$$t.transform;
                 distance = $$anonymous$$t.distance;
                 $$anonymous$$tPoint = $$anonymous$$t.point;
             }
         }
 
         // closestHit is now either still null (i.e. we $$anonymous$$t not$$anonymous$$ng) OR it contains the closest t$$anonymous$$ng that is a valid t$$anonymous$$ng to $$anonymous$$t
 
         return closestHit;
 
     }
 }


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponData : MonoBehaviour
 {
 
     public float fireRate = 0.1f;
     public float damage = 25f;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }




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

0 Replies

· Add your reply
  • Sort: 

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

172 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 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

How do i implement a ammo counter into my gun script 0 Answers

Why is my animation not working? 1 Answer

Need help to understand Unity's logic or C# ? 1 Answer

Pop Up Text while Looking with Gaze Time Function 0 Answers

I want my code to tell the AI to move from one log then back to the fire chamber and then to the other log but it just moves to the first log and doesnt do anything did i code something wrong? 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