Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
  • 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 this is for a fast firing gun can anyone help me fix this 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.GetComponentInChildren<WeaponData>();
             if (weaponData == null)
             {
                 Debug.Log("Did not find any WeaponData in our children!");
                 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 hitTransform;
         Vector3 hitPoint;
 
         hitTransform = FindClosestHitObject(ray, out hitPoint);
 
         if (hitTransform != null)
         {
             Debug.Log("We hit: " + hitTransform.name);
 
             // We could do a special effect at the hit location
             // DoRicochetEffectAt( hitPoint );
 
             Health h = hitTransform.GetComponent<Health>();
 
             while (h == null && hitTransform.parent)
             {
                 hitTransform = hitTransform.parent;
                 h = hitTransform.GetComponent<Health>();
             }
 
             // Once we reach here, hitTransform may not be the hitTransform we started with!
 
             if (h != null)
             {
                 // This 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(hitPoint);
             }
         }
         else
         {
             // We didn't hit anything (except empty space), but let's do a visual FX anyway
             if (fxManager != null)
             {
                 muzzleFlash.SetActive(true);
                 hitPoint = Camera.main.transform.position + (Camera.main.transform.forward * 100f);
 
                 DoGunFX(hitPoint);
 
             }
 
         }
         
         cooldown = weaponData.fireRate;
         
     }
 
 
     void DoGunFX(Vector3 hitPoint)
     {
         
         WeaponData weaponData = gameObject.GetComponentInChildren<WeaponData>();
 
         fxManager.GetComponent<PhotonView>().RPC("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint);
 
     }
 
     Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint)
     {
 
         RaycastHit[] hits = Physics.RaycastAll(ray);
 
         Transform closestHit = null;
         float distance = 0;
         hitPoint = Vector3.zero;
 
         foreach (RaycastHit hit in hits)
         {
             if (hit.transform != this.transform && (closestHit == null || hit.distance < distance))
             {
                 // We have hit something that is:
                 // a) not us
                 // b) the first thing we hit (that is not us)
                 // c) or, if not b, is at least closer than the previous closest thing
 
                 closestHit = hit.transform;
                 distance = hit.distance;
                 hitPoint = hit.point;
             }
         }
 
         // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
 
         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.

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

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

Detecting Collision is not working 1 Answer

Enemy kill counter only counts first killed enemy 0 Answers

Why is my animation not working? 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