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 DeathSammich · Nov 13, 2013 at 05:49 AM · shootinggun

Timing Between each Gun Shot

I have a script for shooting and it shoots way to fast. how can i change the fire rate?

Here is the script:

 var projectilePrefab:Transform;
 var projectile2Prefab:Transform;
 var strayFactor : int = 2; //for example
 var strayFactorAim : int = 0.1; //for example
 private var aiming : boolean = false;
 
 function Update() { 
     if(Input.GetButton("Fire2")){
        aiming = true;
     }
     else{
        aiming = false;
     }
 
     if(aiming == false){
        if(Input.GetButton("Fire1") ) { 
            var randomNumberX = Random.Range(-strayFactor, strayFactor);
            var randomNumberY = Random.Range(-strayFactor, strayFactor);
            var randomNumberZ = Random.Range(-strayFactor, strayFactor); 
            var projectile = Instantiate(projectilePrefab, transform.position, transform.rotation);
            projectile.transform.Rotate(randomNumberX, randomNumberY, randomNumberZ);
            projectile.rigidbody.AddForce(projectile.transform.forward * 10000);
       }
     }
 
     if(aiming == true){
         if(Input.GetButton("Fire1") ) {
            var randomNumberX2 = Random.Range(-strayFactorAim , strayFactorAim );
            var randomNumberY2 = Random.Range(-strayFactorAim , strayFactorAim );
            var randomNumberZ2 = Random.Range(-strayFactorAim , strayFactorAim );
            var projectile2 = Instantiate(projectile2Prefab, transform.position, transform.rotation);
            projectile2.transform.Rotate(randomNumberX2, randomNumberY2, randomNumberZ2);
            projectile2.rigidbody.AddForce(projectile2.transform.forward * 10000);
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Nov 13, 2013 at 05:57 AM

your problem is that you are using Input.GetButton so every frame you get a new projectile.

You need to refrain that:

 var fireRate:float;
 var timer:float;
 
 if(Input.GetButton("Fire1") && timer <= 0){
     //Your shooting
     timer = fireRate;
 }else{
     timer -= Time.deltaTime;
 }
 if(Input.GetButtonUp("Fire1"))
 {
     timer = 0;
 }

fireRate is the frequency of shooting, when shooting timer becomes fireRate and you shoot. Then next round timer is greter than 0 so you cannot shoot but timer gets decreased. This will be until timer is smaller or equal to 0.

If you release the button, then timer is 0 so you can shoot without waiting.

Comment
Add comment · Show 2 · 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 DeathSammich · Nov 18, 2013 at 07:47 AM 0
Share

i need ful lauto not semi auto

avatar image fafase · Nov 18, 2013 at 10:45 AM 0
Share

What do you consider to be full auto? This code should shoot at a rate as long as you are pressing, semi auto is when you need to release each time to shoot. The last one is burst which shoots a limited amount of bullet per press of the trigger (I sound like a NRA member now...or a COD geek). So which are you looking for?

avatar image
0

Answer by rooted · Nov 18, 2013 at 11:02 AM

Check out my post here for my search for a better machine gun. I'm quite happy the way it turned out. It's a bit tricky but very effective.

The Perfect Autofire Script for a Machine Gun

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

17 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 avatar image

Related Questions

Basic Shooting Game 1 Answer

Bullet Script Help 3 Answers

Bullet does not move forward 1 Answer

A node in a childnode? 1 Answer

Animation & Script Help 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