Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Bastin-Ross · Sep 03, 2015 at 05:06 AM · shootingplayer movementammo

Player will not stop shooting when ammo count = 0

My player continues shooting even when ammo = 0. Does anyone know how I Can get it to stop shooting when this happens? Thank you in advance.

     public int Ammo;
 public int maxAmmo = 100;
 public Text text;
 PlayerShooting playerShooting;



 // Use this for initialization
 void Start () {
     text = GetComponent <Text> (); 
     playerShooting = GetComponent <PlayerShooting> ();
     Ammo = maxAmmo;
 }
 
 // Update is called once per frame
 void Update () {
 
     if (Input.GetButton ("Fire1") && Ammo > 0) 
         {
             text.text = Ammo - 1 + "/" + "100";
             Ammo -= 1;
         }
     if (Ammo <= 0) {
         playerShooting.enabled = false;
     }

 }

This is my player movement script if it helps.

     public int damagePerShot = 20;                  // The damage inflicted by each bullet.
     public float timeBetweenBullets = 0.15f;        // The time between each shot.
     public float range = 100f;                      // The distance the gun can fire.
     
     float timer;                                    // A timer to determine when to fire.
     Ray shootRay;                                   // A ray from the gun end forwards.
     RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
     int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
     ParticleSystem gunParticles;                    // Reference to the particle system.
     LineRenderer gunLine;                           // Reference to the line renderer.
     AudioSource gunAudio;                           // Reference to the audio source.
     Light gunLight;                                 // Reference to the light component.
     float effectsDisplayTime = 0.2f;                // The proportion of the timeBetweenBullets that the effects will display for.
 
     void Awake ()
     {
         // Create a layer mask for the Shootable layer.
         shootableMask = LayerMask.GetMask ("Shootable");
         
         // Set up the references.
         gunParticles = GetComponent<ParticleSystem> ();
         gunLine = GetComponent <LineRenderer> ();
         gunAudio = GetComponent<AudioSource> ();
         gunLight = GetComponent<Light> ();
     }
     
     void Update ()
     {
         // Add the time since Update was last called to the timer.
         timer += Time.deltaTime;
         
         // If the Fire1 button is being press and it's time to fire...
         if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets)
         {
             // ... shoot the gun.
             Shoot ();
         }
         
         // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
         if(timer >= timeBetweenBullets * effectsDisplayTime)
         {
             // ... disable the effects.
             DisableEffects ();
         }
     }
     
     public void DisableEffects ()
     {
         // Disable the line renderer and the light.
         gunLine.enabled = false;
         gunLight.enabled = false;
     }
     
     void Shoot ()
     {
         // Reset the timer.
         timer = 0f;
         
         // Play the gun shot audioclip.
         gunAudio.Play ();
         
         // Enable the light.
         gunLight.enabled = true;
         
         // Stop the particles from playing if they were, then start the particles.
         gunParticles.Stop ();
         gunParticles.Play ();
         
         // Enable the line renderer and set it's first position to be the end of the gun.
         gunLine.enabled = true;
         gunLine.SetPosition (0, transform.position);
         
         // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
         shootRay.origin = transform.position;
         shootRay.direction = transform.forward;
         
         // Perform the raycast against gameobjects on the shootable layer and if it hits something...
         if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
         {
             // Try and find an EnemyHealth script on the gameobject hit.
             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
             
             // If the EnemyHealth component exist...
             if(enemyHealth != null)
             {
                 // ... the enemy should take damage.
                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
             }
             
             // Set the second position of the line renderer to the point the raycast hit.
             gunLine.SetPosition (1, shootHit.point);
         }
         // If the raycast didn't hit anything on the shootable layer...
         else
         {
             // ... set the second position of the line renderer to the fullest extent of the gun's range.
             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
         }
     }
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Positive7 · Sep 03, 2015 at 06:19 AM

Set up a bool. Something like this (Sent from mobile so not tested) :

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class test3 : MonoBehaviour {
 
     public int Ammo;
     public int maxAmmo = 100;
     public Text text;
     bool canReload = false;
     PlayerShooting playerShooting;
 
     void Start () {
         text = GetComponent <Text> (); 
         playerShooting = GetComponent <PlayerShooting> ();
         Ammo = maxAmmo;
     }
 
     void Update () {
         text.text = Ammo + "/" + "100";
         if (Input.GetButton ("Fire1") && Ammo > 0) 
         {
             Ammo -= 1;
             if(Ammo <= 0){
                 canReload = true;
             }
         }
         if (Ammo == 0 && canReload) {
             canReload = false;
             Debug.Log("Press R to Reload");
         }
         if(Input.GetKeyDown(KeyCode.R)){
             Ammo = maxAmmo;
         }
     }
 }
Comment
Add comment · Show 1 · Share
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 Bastin-Ross · Sep 04, 2015 at 03:54 AM 0
Share

Firstly, Thankyou for your reply. I tried your recommended script but the player continues to shoot when ammo=0 and I'm not sure how to stop this from happening.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How make the "player" shoot on the Y axis? 0 Answers

Why did my boolean don't work? 0 Answers

[CLOSED] {SOLVED} how could i add an ammo script to this? 1 Answer

How To Do Basic 2D Movement? 1 Answer

Using the camera to follow a simplified 3d spaceship 2 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