Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 19, 2014 at 06:08 AM by fafase for the following reason:

Other

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

Machine gun help

I have a script for a machine 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 within a second. The machine gun should be more to shooting and stoping when button is help and released and shoot per half a second.

Please help me.

This 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 hit, 10)){
             //Declear new GameObject variable
             GameObject BulletHole;
             //Create the shootmark prefab at the hit point
             BulletHole = Instantiate (BulletMark, hit.point+(hit.normal*0.001f), transform.rotation) as GameObject;
             //Rotate the created object at the face of the normal LookAt (hit.point+hit.normal)
             BulletHole.transform.LookAt(hit.point+hit.normal);
             BulletHole.transform.parent=hit.transform;
             hit.transform.gameObject.SendMessage("Hit", 50, SendMessageOptions.DontRequireReceiver);
     
     }
Comment
Add comment · 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 $$anonymous$$us', 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 (assu$$anonymous$$g 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 $$anonymous$$us 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
0
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
Add comment · 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 $$anonymous$$us 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...

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

On Collide Destroy Game Object 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