- Home /
How come when i reach the max bullets i can still shoot?,How come when i shoot the max amount of bullets it still lets me shoot.
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SingleShotgun : Gun
{
[SerializeField] Camera cam;
public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 2f;
private bool isReloading = false;
public Animator animator;
PhotonView PV;
void Start()
{
currentAmmo = maxAmmo;
}
void Update()
{
if(isReloading)
return;
if(currentAmmo <= 0)
{
StartCoroutine(Reload());
}
}
void Awake()
{
PV = GetComponent<PhotonView>();
}
public override void Use()
{
Shoot();
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reloading...");
animator.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool("Reloading", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
currentAmmo--;
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
ray.origin = cam.transform.position;
if(Physics.Raycast(ray, out RaycastHit hit))
{
hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);
PV.RPC("RPC_Shoot", RpcTarget.All, hit.point, hit.normal);
}
}
[PunRPC]
void RPC_Shoot(Vector3 hitPosition, Vector3 hitNormal)
{
Collider[] colliders = Physics.OverlapSphere(hitPosition, 0.3f);
if(colliders.Length != 0)
{
GameObject bulletImpactObj = Instantiate(bulletImpactPrefab, hitPosition + hitNormal * 0.001f, Quaternion.LookRotation(hitNormal, Vector3.up) * bulletImpactPrefab.transform.rotation);
Destroy(bulletImpactObj, 7.5f);
bulletImpactObj.transform.SetParent(colliders[0].transform);
}
}
}
, using Photon.Pun; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SingleShotgun : Gun
{
[SerializeField] Camera cam;
public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 2f;
private bool isReloading = false;
public Animator animator;
PhotonView PV;
void Start()
{
currentAmmo = maxAmmo;
}
void Update()
{
if(isReloading)
return;
if(currentAmmo <= 0)
{
StartCoroutine(Reload());
}
}
void Awake()
{
PV = GetComponent<PhotonView>();
}
public override void Use()
{
Shoot();
}
IEnumerator Reload()
{
isReloading = true;
Debug.Log("Reloading...");
animator.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool("Reloading", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
currentAmmo--;
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
ray.origin = cam.transform.position;
if(Physics.Raycast(ray, out RaycastHit hit))
{
hit.collider.gameObject.GetComponent<IDamageable>()?.TakeDamage(((GunInfo)itemInfo).damage);
PV.RPC("RPC_Shoot", RpcTarget.All, hit.point, hit.normal);
}
}
[PunRPC]
void RPC_Shoot(Vector3 hitPosition, Vector3 hitNormal)
{
Collider[] colliders = Physics.OverlapSphere(hitPosition, 0.3f);
if(colliders.Length != 0)
{
GameObject bulletImpactObj = Instantiate(bulletImpactPrefab, hitPosition + hitNormal * 0.001f, Quaternion.LookRotation(hitNormal, Vector3.up) * bulletImpactPrefab.transform.rotation);
Destroy(bulletImpactObj, 7.5f);
bulletImpactObj.transform.SetParent(colliders[0].transform);
}
}
}
Comment