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 /
avatar image
0
Question by hopper · Oct 02, 2014 at 10:13 AM · c#fpsweapon

FireWeapon.cs not working

I have a script and whenever I hit the "Fire1" button, it doesn't work.

Whats currently happening is its firing one bullet and then firing it again in a second.

Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class FireWeapon : MonoBehaviour 
 {
     public float fireRate = 0f;
     public Rigidbody bullet;
     public float speed = 0f;
 
     void Update ()
     {
         if (Input.GetButtonDown ("Fire1")) 
         {
             InvokeRepeating("Fire", 1f, fireRate);
         }
     }
 
     void Fire ()
     {
         Rigidbody pel = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
         pel.AddForce (transform.forward * speed);
     }
 }
 
Comment
Add comment · Show 3
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 Josh Naylor ♦♦ · Oct 02, 2014 at 10:15 AM 0
Share

Because that's exactly what you are telling it to do. http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html

avatar image hopper · Oct 06, 2014 at 02:23 AM 0
Share

Thanks for that reference @JoshNaylor. Now I have rewritten the script with the CancelInvoke() function but it spawns hundreds of them at a time. Here is the script currently:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponScript : $$anonymous$$onoBehaviour 
 {
     public Rigidbody bullet;
     public int ammo;
     public float fireRate = 0f;
     public float speed = 0f;
 
     void Update ()
     {
         if(Input.GetButton("Fire1"))
         {
             InvokeRepeating("Fire", 0f, fireRate);
         }
 
         else if(Input.GetButtonUp("Fire1"))
         {
             CancelInvoke("Fire");
         }
     }
 
     void Fire ()
     {
         Rigidbody pel = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
         pel.AddForce (transform.forward * speed);
     }
 }
avatar image Omegathorion · Oct 06, 2014 at 02:27 AM 0
Share

It's going to spawn hundreds of them because you're doing it in Update, and Update runs every frame, and Unity processes a lot of frames per second.

There's a middle ground that you're getting to. Why not say something like InvokeRepeating("Fire", 0.1f, fireRate)? That effectively means you're firing 10 bullets per second. You can use your InvokeRepeating time parameter to control your weapon's rate of fire. But if you tell it to InvokeRepeating every 0 seconds, that effectively means your weapon has an infinite rate of fire, so that's why Unity is spawning hundreds of them.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by N1warhead · Oct 06, 2014 at 03:21 AM

Something like this will work.

 public bool Activate = false;
 
 if(Input.GetButton("Fire1")){
 StartCoroutine (Shooting (1.0f));
         }
 }
 
 
     
 
     IEnumerator Shooting(float waitTime){
         Activate = true;
         yield return new WaitForSeconds (waitTime);
         GameObject clone = Instantiate (FX120P, Barrel.position, Barrel.rotation)as GameObject;
         audio.PlayOneShot (ShootSFX);
         Activate = false;
     }
 }
 
 

I got that from my code, it should shoot like every 1 second, change to your liking, I hope that works for you! P.S. - Change the FX120P to whatever your gameobject is called.

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 MitchWardle180 · Jun 03, 2015 at 01:15 PM

Try adding these to variables to your script

 public float coolDownTime = 0.25f;
 float timer;

then add this line and make sure it the first line in the update function

 timer += Time.deltaTime;

then when you check for input you can also check if the timer is more than the cool down time:

 if(Input.GetMouseButton(0) && timer >= coolDownTime){
 timer = 0;
 Shoot();
 }
 
 void Shoot(){
 //stop audio source
 //set audio source clip to shoot sound
 //play audio source
 //
 //CODE TO INSTATIATE BULLET OR RAYCAST
 //
 }



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.

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

C# Switch Weapon 2 Answers

pause for dastardly bannana 1 Answer

Weapon Switching 2 Answers

HDRP Camera weapon layer 1 Answer

Stop animation and back to default location 3 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