My gun wont stop firing
My gun wont stop firing can someone help me thanks. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class AmmoDisplay : MonoBehaviour { public int ammo; public bool isFiring; public Text ammoDispay;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
ammoDispay.text = ammo.ToString(); // Display On Your Current Ammo
if(Input.GetMouseButtonDown(0) && !isFiring && ammo > 0)
{
isFiring = true;
ammo--;
isFiring = false;
}
if (Input.GetKeyDown(KeyCode.R))
{
ammo = 25;
}
if (ammo <= 0)
{
isFiring = false;
}
}
}
Change isFiering to canFire
Also in mouse event change to canFire without " ! "
Like this
if(Input.GetMouseButtonDown(0) && canFire && ammo > 0){
ammo --;
}
nothing else in mouse event
And when you press r make
canFire = true;
ammo = 25;
Than it should do it
If I remove ! it won't subtract my ammo it just stays at 25.
No
The !
Is checking the canFire if it's false
So if you remove !
It checks if canFire is true
And its still firing if its 0 I will give you another script maybe its from my bulletimpact I am probably not firing I just can see my bulletimpact
using Photon.Pun; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SingleShotGun : Gun { [SerializeField] Camera cam;
PhotonView PV;
void Awake()
{
PV = GetComponent<PhotonView>();
}
public override void Use()
{
Shoot();
}
void Shoot()
{
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, 10f);
bulletImpactObj.transform.SetParent(colliders[0].transform);
}
}
}
Can't see AmmoDispley in here so i think it's not related. To this script.
I posted how code should look like
Tho you will empty your gun in about a second