• 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 /
avatar image
0
Question by gtagamer · Nov 08, 2014 at 02:38 AM · shooting

why does my gun stop shooting

hello everyone i really serious need help.Ok im making this game and i got two gun shooting scripts and when ever i shoot them its shoots out like 6 bullets and then it stops shooting this is a serious problem could someone help me please.

c# scripts:

shoot: using UnityEngine; using System.Collections;

 public class Shoot : MonoBehaviour {
 
     public GameObject bullet;
     public GameObject bulletHole;
     public float delayTime = 0.5f;
     
     private float counter = 0;
     
     void FixedUpdate ()    
      {
         if(Input.GetKeyDown(KeyCode.Mouse0) && counter > delayTime)
         {
             Instantiate(bullet, transform.position, transform.rotation);
             audio.Play();
             counter = 0;
             
             RaycastHit hit;
             Ray ray = new Ray(transform.position, transform.forward);
             if(Physics.Raycast(ray, out hit, 100f))
             {
                 Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
             }
         }
         counter += Time.deltaTime;
     }
 }
 


bullet move script:

 using UnityEngine;
 using System.Collections;
 
 public class MoveBullet : MonoBehaviour {
 
     public float speed = 1f;
 
     void Start ()    
      {
         Destroy(gameObject, 5f); //Delete the bullet after 5 seconds
     }
     
     void Update ()    
      {
         transform.Translate(0, 0, speed);
     }
 }
 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by VisalDXP · Nov 08, 2014 at 05:44 AM

Try this:

 public class Shoot : MonoBehaviour {
  
      public GameObject bullet;
      public GameObject bulletHole;
      public float delayTime = 0.5f;
      
      private float counter = 0;
      
      void Update ()    
       {
          //for rapid fire you could use this
          //if( Input.GetKey(KeyCode.Mouse0) && counter > delayTime )

          if(Input.GetKeyDown(KeyCode.Mouse0) && counter > delayTime)
          {
              Instantiate(bullet, transform.position, transform.rotation);
              audio.Play();
              counter = 0;
              
              RaycastHit hit;
              Ray ray = new Ray(transform.position, transform.forward);
              if(Physics.Raycast(ray, out hit, 100f))
              {
                  Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
              }
          }
          counter += Time.deltaTime;
      }
  }


just like @Linus mentioned above, you should use Update instead of FixedUpdate. I think FixedUpdate is suppose to be used for physics calculation only.

Comment
Add comment · Show 5 · 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 gtagamer · Nov 08, 2014 at 11:58 PM 0
Share

nope still runs out of bullets

avatar image b1gry4n · Nov 09, 2014 at 12:12 AM 0
Share

if it "runs out of bullets" you have another script interfering. There is nothing in the above scripts that would cause it to stop shooting other than pausing the game.

avatar image Bunny83 · Nov 09, 2014 at 12:33 AM 0
Share

@gtagamer: Uhm, have you actually realized that your script is a single-shot script? For each click it will fire one bullet and you can't click faster than your delayTime.

If you want an automatic gun you should replace your Get$$anonymous$$eyDown (which only returns true for one frame) with Get$$anonymous$$ey (which returns true as long as the key / button is pressed down).

edit
Oh and make sure you don't have this script on multiple objects. Otherwise all of them will shoot when you press your mouse button.

avatar image gtagamer · Nov 09, 2014 at 01:42 PM 0
Share

but its like the first 6secs of the game it would shoot like 7bullets and then the gun completely stop shooting and i dont know what to do

avatar image Linus · Nov 09, 2014 at 02:15 PM 0
Share

I converted your answer to a comment. I guessed it was this answer you commented on.

By the way I update my answer.

avatar image
1

Answer by Linus · Nov 08, 2014 at 04:19 AM

Change FixedUpdate with Update

Whaen taking user input you need to use Update. As FixedUpdate is not every frame. And will only work if the giving input that exact frame.

Edit: Did some testing with your script. I still think you should use Update for gathering input. But the problem is that the bullets collide with each other. Well they dont collide but their raycast hits another bullet.

To test, try with a delay time of say 2 seconds. Or shorten the length of this raycast

 if (Physics.Raycast(ray, out hit, 100f)

Best solution I think is using a layermask. see http://answers.unity3d.com/questions/148205/using-layer-mask-in-raycast.html http://stackoverflow.com/questions/24563085/raycast-but-ignore-yourself

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

Answer by SkaredCreations · Nov 08, 2014 at 03:00 AM

Try this:

 public class Shoot : MonoBehaviour {
 
     public GameObject bullet;
     public GameObject bulletHole;
     public float delayTime = 0.5f;
     private float counter = 0;
      
     void FixedUpdate ()    
     {
         if(Input.GetKeyDown(KeyCode.Mouse0) && Time.time >= counter)
         {
             Instantiate(bullet, transform.position, transform.rotation);
             audio.Play();
             counter = Time.time + delayTime;
 
             RaycastHit hit;
             Ray ray = new Ray(transform.position, transform.forward);
             if(Physics.Raycast(ray, out hit, 100f))
             {
                 Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
             }
         }
      }
 }
 
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 gtagamer · Nov 08, 2014 at 04:14 AM 0
Share

nope didnt fix the problem

avatar image
0

Answer by Ali-hatem · Nov 09, 2014 at 02:37 PM

  public class Shoot : MonoBehaviour 
      {
         public GameObject bullet;
         public GameObject bulletHole;
     
         public float delayTime = 0.5f;
         private float counter ;
     
         void Update ()
         {
            if(Input.GetKeyDown(KeyCode.Mouse0) && Time.time > counter )
            {
               Instantiate(bullet, transform.position, transform.rotation);
               audio.Play();
               counter = Time.time + delayTime;

               RaycastHit hit;
               Ray ray = new Ray(transform.position, transform.forward);

               if(Physics.Raycast(ray, out hit, 100f))
               {
                  Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
               }
            }
         
         }
      }




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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help with a Script in C# for shooting and lighting 1 Answer

Unity animations not working 1 Answer

Need help with shooting script 2 Answers

Very simple picking up items script? 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