• 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
Question by Angela0223 · Nov 29, 2021 at 11:54 AM · firingwont stop

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;
     }
 }

}

Comment

People who like this

0 Show 15
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
avatar image MilitaryG · Nov 29, 2021 at 12:13 PM 0
Share

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

avatar image Angela0223 MilitaryG · Nov 29, 2021 at 12:43 PM 0
Share

If I remove ! it won't subtract my ammo it just stays at 25.

avatar image MilitaryG Angela0223 · Nov 29, 2021 at 12:47 PM 0
Share

No

The !

Is checking the canFire if it's false

So if you remove !

It checks if canFire is true

Show more comments
avatar image Angela0223 MilitaryG · Nov 29, 2021 at 12:46 PM 0
Share

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

avatar image Angela0223 MilitaryG · Nov 29, 2021 at 12:46 PM 0
Share

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);
     }
 }

}

avatar image MilitaryG Angela0223 · Nov 29, 2021 at 01:01 PM 0
Share

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

Show more comments

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

170 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

Related Questions

[Unity2D] Firing bullets based on gun orientation 1 Answer

Get Opposite of a Rotation Stored In a Quaternion 0 Answers

Trouble with both rapid fire and also firing on control in fps game. 0 Answers

2 Guns aren't meant to fire together but they do 0 Answers

Need help with Vehicle Movement, Collisions, Firing. 0 Answers


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