Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DTJ Productions · Apr 20, 2010 at 07:02 PM · gunshootmouseclick

Want to Fire Machine Gun

Hey, I would like to click with the mouse and say bullets fly out, I want it so tht if I hold down the mouse key alot of machine gun bullets fly out and never end until I let go Whats the scripting name of "left mouse click" also what do I put in Javascript that means "fire until button let go." Something like that.

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
5
Best Answer

Answer by spinaljack · Apr 20, 2010 at 07:30 PM

If you use Input.GetKeyDown then it'll only fire once but if you use Input.GetKey then it'll tell you if the key is held down. Depending on how you're making bullets come out it'd be something like:

 // Instantiate a rigidbody then set the velocity
 var projectile : Rigidbody;
 function Update () {
     // Ctrl was pressed, launch a projectile
     if (Input.GetButton("Fire1")) {
         // Instantiate the projectile at the position and rotation of this transform
         var clone : Rigidbody;
         clone = Instantiate(projectile, transform.position, transform.rotation);
 
         // Give the cloned object an initial velocity along the current 
         // object's Z axis
         clone.velocity = transform.TransformDirection (Vector3.forward * 10);
     }
 }

             
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 Eric5h5 · Apr 21, 2010 at 01:01 AM 0
Share

That would make a new bullet every frame. So your rate of fire would be framerate-dependent.

avatar image spinaljack · Apr 21, 2010 at 01:37 AM 1
Share

That's true, but he wasn't specific.

avatar image
4

Answer by 3dDude · Apr 21, 2010 at 01:40 PM

or you could use Time.time like :

 var fireRate : float = 0.1;
 var projectile : Rigidbody;
 private var nextFire = 0.0;
 function Update () {
     if(Input.GetKey("mouse 0")&&Time.time > nextFire){
        nextFire = Time.time + fireRate;
        var copy = Instantiate(projectile,transform.position,transform.rotation);
        copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward);
    }
 }

i like this code better then Invoke.

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 scarletsnake · Dec 01, 2012 at 01:33 AM 0
Share

Yours is the quickets and easiest method that can be applied and tweaked at will. Thanks a lot!

avatar image
1

Answer by Eric5h5 · Apr 21, 2010 at 01:04 AM

Use InvokeRepeating:

 var rateOfFire = .1;
 
 function Update () {
    if (Input.GetButtonDown("Fire1"))
       InvokeRepeating("Shoot", .001, rateOfFire);
    if (Input.GetButtonUp("Fire1"))
       CancelInvoke("Shoot");
 }
 
 function Shoot () {
    // Instantiate a bullet here
 }

             
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 lion-gv · Nov 10, 2010 at 12:23 AM

Here's my solution for rate of fire:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public float rateOfFire;
     public GameObject primaryProjectile;
     private float rateOfFireTimer = 0.0f;
 
     void Update () {
 
         //this is a counter that tells you the time in seconds from the last projectile fired
         rateOfFireTimer += Time.deltaTime;
 
         //this bit isn't necessary but I did it for debugging purposes
         print (rateOfFireTimer);
 
         //Fire Primary Weapon. I do (1/rateOfFire) so that you can enter a number like 10 in the inspector to mean 10 per second
         if (Input.GetButton ("Fire1") && rateOfFireTimer >= (1/rateOfFire))
             FirePrimaryWeapon();
     }
 
     void FirePrimaryWeapon() {
         //this fires the projectile
         Instantiate (primaryProjectile, transform.position, Quaternion.identity);
 
         //this resets the timer
         rateOfFireTimer = 0.0f;
     }

             
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I make a gun project a particle? 1 Answer

Limited Fire Rate? 1 Answer

Shooting script not working properly 3 Answers

How to add a delay to my script. 3 Answers

Using the arrow keys to shoot using c# 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