• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by CrilleStyles · Aug 18, 2014 at 11:13 AM · fpsgunsnipershotgun

How to make my gun semi-auto?

I want to know how to make my gun semi auto instead of full automatic? I've tried to use a boolean to turn of the inovake but it did not work. I try to make a shotgun/sniper/rifle and wonder if anyone knows how to make it? Here's the script:

 #pragma strict
 var reloadSound: AudioClip;
 var fireSound: AudioClip;
 var NoAmmoSound : AudioClip;
  
 var bullet : GameObject;
 var scope: GameObject;
 var laser: GameObject;
 var silencer: GameObject;
 
 private var hasScope: boolean;
 private var hasLaser: boolean;
 private var hasSilencer: boolean;
 
  
 var bulletsPerSecond = 14.0;
 private var shooting = false;
 var bulletForce = 1000.0;
 var bulletsInMag = 30;
 var magsLeft = 10;
 var LoadAmmount = 30;
 private var reloading = false;
 var reloadCount = 0;
  
 var otherObj : GameObject;
 var MainCamera : Transform;
 
 var style : GUIStyle;
  
 function Start() {
   hasScope = Random.value < 0.35; // scope chance: 20%
   hasLaser = Random.value < 0.25; // laser chance: 35%
   hasSilencer = Random.value < 0.15; // silencer chance: 25%
   scope.SetActiveRecursively(hasScope);
   laser.SetActiveRecursively(hasScope);
   silencer.SetActiveRecursively(hasScope);
 }
 {
    InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
 }
 function Shoot() {
   if (!shooting) return;
     var go = Instantiate(bullet, transform.position, transform.rotation);
     go.rigidbody.AddRelativeForce(Vector3.up * bulletForce);
     audio.PlayOneShot(fireSound);
     bulletsInMag--;
  }
 function Update(){
     shooting = false;
     Debug.Log(bulletsInMag);
     if(Input.GetAxis("Fire1") && bulletsInMag > 0 && magsLeft > -1 && !reloading){
         shooting = true;
         }else if(bulletsInMag <= 0 && magsLeft <= 0 && Input.GetMouseButton(0)){
         audio.PlayOneShot(NoAmmoSound);
     }else if(bulletsInMag <= 0 && magsLeft > 0){
     shooting = false;
     Reload();
     }
 }
 function Reload(){
     reloading = true;
     otherObj.GetComponent(Animations).enabled = false;
     otherObj.animation.CrossFade ("Reload");
     audio.PlayOneShot(reloadSound);
     bulletsInMag = LoadAmmount;
     magsLeft -= 1;
     yield WaitForSeconds(2);
     reloading = false;
     otherObj.GetComponent(Animations).enabled = true;
 }
 
 function OnGUI(){
 GUI.Box  (Rect (Screen.width - 300,Screen.height-35,200,80), bulletsInMag.ToString(), style);
 GUI.Label (Rect (Screen.width - 200,Screen.height-35,200,80), magsLeft.ToString(), style);
 }
Comment
Add comment · Show 5
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 arain55 · Aug 18, 2014 at 11:26 AM 0
Share

I ment 2 ways and the second way is to make fire rate varieble and the decrease when you press a button that enable semi auto

avatar image CrilleStyles · Aug 18, 2014 at 11:30 AM 0
Share

I'll use it thank you!

avatar image arain55 · Aug 18, 2014 at 11:33 AM 1
Share

Your welcome tell me if it works if not ill get something else

avatar image arain55 · Aug 18, 2014 at 11:59 AM 0
Share

Did it work ??

avatar image Kaz_Yamof · Aug 18, 2014 at 12:38 PM 0
Share

You could use Coroutines to do this.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by arain55 · Aug 18, 2014 at 11:24 AM

Just a suggestion

you can do t$$anonymous$$s to ways one way you can make a new if statement saying that if you press not hold the left mouse button you shoot w$$anonymous$$ch would mean that you have to press it once to shoot eg..

 If(Input.GetKey(KeyCode.C)) // t$$anonymous$$s is the key that enables semi auto
 {
      If(Input.GetButtonDown("fire1")){     //the buttondown part is important
          
        //then put you shoot paragraph here
  
      }
 
 }
 
 I may have some mistakes since I wrote it on my phone

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

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

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

Related Questions

How do I put a delay on a gunshot? 2 Answers

Bullet Collision Issues 1 Answer

Particles not appearing on impact, muzzle flash problems? 2 Answers

Weapon random movement 0 Answers

Aim Down Sights Positioning Problem 1 Answer


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