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
1
Question by $$anonymous$$ · Mar 31, 2013 at 07:01 AM · gunguns

How Do I Make My Gun JUST Semi-AutoMatic?

I Have a script set up here.

var projectile : Rigidbody; var speed = 5;

function Update () {

 if ( Input.GetButton ("Fire1")) {
 clone = Instantiate(projectile, transform.position, transform.rotation);
 clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
 
 Destroy (clone.gameObject, 4);
 
 
 }}

But How Do I make it semi auto? any help please?

Comment
Add comment · Show 1
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 DavidDebnar · Mar 31, 2013 at 08:00 AM 0
Share

Use Input.GetButtonDown () ins$$anonymous$$d. It's only called once per mouse click.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ExpiredIndexCard · Mar 31, 2013 at 07:03 AM

Change the Input.GetButton to Input.GetButtonDown It only checks for when the button gets pressed. Hope that helps :)

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
1

Answer by Scribe · Apr 01, 2013 at 12:17 AM

This should work for you:

 private var shooting = false;
 var shotSpacer = 0.1; //time between shots in burst
 var burstSpacer = 0.5; //how long after a firing burst you must wait to fire another
 var burstShots = 3; //number of shots in a burst
 private var remainingShots : int;
 var projectile : Rigidbody; //your bullet object
 var speed = 5;
 
 function Update () {
     if(Input.GetButtonDown("Fire1") && !shooting){
         BurstFire ();
     }
 }
 
 function BurstFire () {
     shooting = true;
     remainingShots = burstShots;
     InvokeRepeating("Fire", 0, shotSpacer);
     yield WaitForSeconds(burstSpacer);
     shooting = false;
 }
 
 function Fire () {
     if(remainingShots > 0){
         clone = Instantiate(projectile, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
         remainingShots --;
     }else{
         CancelInvoke();
     }
 }

by default it will fire a burst of 3 bullets but you can change the variables to suit your needs.

Scribe

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 ExpiredIndexCard · Mar 31, 2013 at 09:27 PM

 canShoot : float = false;
 
 coolDown : float = 0;
 
 coolDownMax : float = 2;
     
 function Update(){
     
 if(coolDown < coolDownMax){
        coolDown += Time.deltaTime;
        canShoot = false;
     }
     
 if(coolDown >= coolDownMax){
        canShoot = true;
     }
     
 if ( Input.GetButton ("Fire1")) {
     
     clone = Instantiate(projectile, transform.position, transform.rotation);
     clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
     canShoot = false;
     coolDown = 0;
      Destroy (clone.gameObject, 4);
      
     }
     
     }

This is untested code but I think it will work. Tell me if there are any errors. Also, the time between each shot is 2 seconds. You can change this by changing the coolDownMax variable. Tell me if you need anything else :)

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Gunfire Help 1 Answer

my bullet holes get stuck in mid air 0 Answers

A node in a childnode? 1 Answer

RayCast Shooting not working 0 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