Advertisement
GetMyIsland

Improved Unity shooting script

Feb 16th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using Photon.Pun;
  7.  
  8. public class weaponScript : MonoBehaviour
  9. {
  10.     public weaponSO weaponStats;
  11.    
  12.     public GameObject reloadingText;
  13.     public TMP_Text ammoValue;
  14.     public Transform firePoint;
  15.     public GameObject bulletPrefab;
  16.     public float bulletForce = 2f;
  17.     public bool isShooting;
  18.     public float pistolDamage;
  19.     public int currentAmmoClip = 1;
  20.     public int maxAmmoClip;
  21.     public int currentReserve = 1;
  22.     public int maxReserve;
  23.     public float reloadTime = 2f;
  24.     public bool isReloading = false;
  25.     public int bulletsShot;
  26.     public float startingDamage;
  27.     PhotonView view;
  28.  
  29.  
  30.  
  31.     public void Start()
  32.     {
  33.         view = this.GetComponent<PhotonView>();
  34.  
  35.         WeaponStats();
  36.         currentAmmoClip = maxAmmoClip;
  37.         currentReserve = maxReserve;
  38.         bulletsShot = maxAmmoClip - currentAmmoClip;
  39.         isShooting = false;
  40.         weaponStats.damage = startingDamage;
  41.  
  42.     }
  43.  
  44.    
  45.     public void Update()
  46.     {
  47.  
  48.         ammoValue.text = currentAmmoClip.ToString("0") + "/" + currentReserve.ToString("0");
  49.         if (Input.GetKeyDown(KeyCode.R))
  50.         {        
  51.             if(currentReserve <= 0)
  52.             {
  53.                 return;
  54.             }
  55.             if (currentAmmoClip == maxAmmoClip && !isReloading)
  56.             {
  57.                 return;
  58.             }
  59.             else
  60.             {
  61.                 StartCoroutine(Reload());
  62.                 return;
  63.             }
  64.         }
  65.         Debug.Log("this works");
  66.         if(Input.GetButtonDown("Fire1") && !isReloading && currentAmmoClip >= 1 && view.IsMine)
  67.         {
  68.             Debug.Log("can shoot");
  69.             Shoot();
  70.         }
  71.  
  72.         if(isReloading)
  73.            return;
  74.  
  75.         if(currentAmmoClip <= 0 && currentReserve >= 1)
  76.         {
  77.            
  78.             StartCoroutine(Reload());
  79.             return;
  80.         }
  81.  
  82.        
  83.     }
  84.     IEnumerator Reload()
  85.     {
  86.  
  87.         reloadingText.SetActive(true);
  88.         isReloading = true;
  89.         yield return new WaitForSeconds(reloadTime);
  90.         if(currentReserve <= bulletsShot)
  91.         {
  92.             currentAmmoClip += currentReserve;
  93.             currentReserve = 0;
  94.         }
  95.         else
  96.         {
  97.             currentReserve -= bulletsShot;
  98.             currentAmmoClip = maxAmmoClip;
  99.         }
  100.         bulletsShot = 0;
  101.         reloadingText.SetActive(false);
  102.  
  103.         isReloading = false;
  104.     }
  105.  
  106.     [PunRPC]
  107.     public void RPC_Shoot()
  108.     {
  109.         Debug.Log("RPC_Shoot works");
  110.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
  111.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
  112.         rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
  113.     }
  114.  
  115.     void Shoot()
  116.     {
  117.         Debug.Log("Shoot() works");
  118.         bulletsShot += 1;
  119.         currentAmmoClip -= 1;
  120.         isShooting = true;
  121.         FindObjectOfType<AudioManager>().Play("shot1");
  122.         view.RPC("RPC_Shoot", RpcTarget.All);
  123.     }
  124.  
  125.     public void WeaponStats()
  126.     {
  127.         pistolDamage = weaponStats.initialDamage;
  128.         maxAmmoClip = weaponStats.maxAmmoClip;
  129.         maxReserve = weaponStats.maxReserve;
  130.         reloadTime = weaponStats.reloadTime;
  131.         bulletForce = weaponStats.bulletForce;
  132.         startingDamage = weaponStats.initialDamage;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement