Advertisement
Guest User

weapon script

a guest
Feb 15th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 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 = 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") && currentAmmoClip >= 1 && view.IsMine)
  67.         {
  68.             Debug.Log("can shoot");
  69.             view.RPC("RPC_Shoot", RpcTarget.AllBuffered);
  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.         if(isReloading == false)
  111.         {    
  112.             bulletsShot += 1;
  113.             currentAmmoClip -= 1;
  114.             isShooting = true;
  115.             Shoot();
  116.             FindObjectOfType<AudioManager>().Play("shot1");
  117.             return;
  118.         }
  119.     }
  120.  
  121.     void Shoot()
  122.     {
  123.         Debug.Log("Shoot() works");
  124.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
  125.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
  126.         rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
  127.     }
  128.  
  129.     public void WeaponStats()
  130.     {
  131.         pistolDamage = weaponStats.initialDamage;
  132.         maxAmmoClip = weaponStats.maxAmmoClip;
  133.         maxReserve = weaponStats.maxReserve;
  134.         reloadTime = weaponStats.reloadTime;
  135.         bulletForce = weaponStats.bulletForce;
  136.         startingDamage = weaponStats.initialDamage;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement