• 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 /
This question was closed Feb 19, 2014 at 06:08 AM by fafase for the following reason:

Other

avatar image
Question by Nourero · Aug 06, 2013 at 01:41 PM · enemybloodmachinegun

Machine gun help

I have a script for a mac$$anonymous$$ne gun but every time I press the fire button the gun keeps shooting and never stops even if there is no more bullets it just goes to minus. I have managed to change it to stop when I release the button but the gun shoots out all the ammo wit$$anonymous$$n a second. The mac$$anonymous$$ne gun should be more to shooting and stoping when button is help and released and shoot per half a second.

Please help me.

T$$anonymous$$s is my script.

 public GameObject BulletMark;    

 public float shotInterval = 0.1f; // interval between shots
 public float reloadTime = 0.5f; // reload time
  public float FireRate;
 public float GunRecoil = 10;
 private float shotTime; // time control

 void Update () {
     if (Bullets>0 && Time.time> nextFire && Input.GetMouseButtonDown(0)){
         nextFire = Time.time + FireRate;
         InvokeRepeating("Shoot", 0.1f, 0.1f);                //Start Shooting
             //GunEffect.active = true;
         }else{
             if (Bullets==0 && AmmoLeft>0 && Input.GetKeyDown("r")){
                 Bullets = Bullets+8;
                 AmmoLeft = AmmoLeft-1;
                 AmmoClips.guiText.text = AmmoLeft.ToString();
                 Ammo.guiText.text = Bullets.ToString();
                 shotTime = Time.time + reloadTime; // set reload time
         }
     }
 }
 
 void Shoot(){
     animation.Play();
     audio.Play();
     //Normal Raycast                    //Add a random value to mouse position(r
         ray = Camera.main.ScreenPointToRay(Input.mousePosition+Random.insideUnitSphere*GunRecoil);
         if(Physics.Raycast(ray, out $$anonymous$$t, 10)){
             //Declear new GameObject variable
             GameObject BulletHole;
             //Create the shootmark prefab at the $$anonymous$$t point
             BulletHole = Instantiate (BulletMark, $$anonymous$$t.point+($$anonymous$$t.normal*0.001f), transform.rotation) as GameObject;
             //Rotate the created object at the face of the normal LookAt ($$anonymous$$t.point+$$anonymous$$t.normal)
             BulletHole.transform.LookAt($$anonymous$$t.point+$$anonymous$$t.normal);
             BulletHole.transform.parent=$$anonymous$$t.transform;
             $$anonymous$$t.transform.gameObject.SendMessage("Hit", 50, SendMessageOptions.DontRequireReceiver);
     
     }
Comment

People who like this

0 Show 2
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 robertbu · Aug 06, 2013 at 02:57 PM 0
Share

I don't spot anything that would account for your behavior in this code, but something is strange here. You say bullets 'goes to minus', but nothing in this code ever decreases the number of bullets. Which means we are missing some critical code to take a look at or you've misunderstood what is going on in your code.

Did you check the initialization of FireRate in the inspector, not just in the code (assuming it is not private)?

You want to remove your second question and ask it separately.

avatar image Nourero · Aug 06, 2013 at 04:13 PM 0
Share

After reading the script now I see that one line is wrong. Shoot(); I changed to InvokeRepeating thats why I get minus and these problems.

I have been trying to add FireRate in almost every way I know but nothing really helps. Do you know how I can add it?

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Seizure · Aug 06, 2013 at 04:00 PM

I'm not seeing where you decrement Bullets. Are you sure Bullets ever == 0? You may want to check any other scripts you have in the scene, try turning them off and see if you still have the same effect.

Comment

People who like this

0 Show 3 · 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 Nourero · Aug 06, 2013 at 04:11 PM 0
Share

There is no other script that is interfering with this one. I have a problem with just the gun script. I changed it to be InvokeRepeate and that made the gun shoot really nicely but like I explained in the description that my bullets go minus and the firing never stops.

avatar image Ziron999 · Feb 19, 2014 at 06:03 AM 0
Share

I think he's right would you want to do: (Bullets>0 || Time.time> nextFire ?

avatar image fafase · Feb 19, 2014 at 06:08 AM 0
Share

I think this is from last summer and probably fixed but since new users can't bother taking care of their question, this is now back up...

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Blood on weapon. 1 Answer

How to make my enemy bleed 1 Answer

How do I use particles to make blood spurt from an enemy when you shoot it? 1 Answer

Enemy bleeding 1 Answer

Player pushback when collide with enemy 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