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 DayyanSisson · Aug 05, 2011 at 10:08 PM · inputshootingshootfire

Hold Button Shooting

I need to make it so that when you hold down "Fire1" the player shoots continuously until you release. Is there a way to do that? Here's the script I'm using to shoot:

 var Laser : GameObject;
 
 function Update () 
 {
     if(Input.GetButtonDown("Fire1"))
     {
         Instantiate(Laser, transform.position, transform.rotation);
     }
 }

The script works but all I need is to make shoot continuously when holding a button down. Please help.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Chris D · Aug 05, 2011 at 10:15 PM

Input.GetButton is what you're after. It's used the same as you have in your code above, but is activated for all frames the button is held down.

Comment
Add comment · Show 4 · 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 DayyanSisson · Aug 06, 2011 at 12:13 AM 0
Share

Ok, but the problem is that too many our firing at a time. Now the lasers are colliding and go in random directions. How do I make so that shoot at a specified rate?

avatar image Chris D · Aug 06, 2011 at 12:27 AM 0
Share

That's a whole new problem. Search through the questions on this site (and on google) for creating a cooldown timer.

avatar image DayyanSisson · Aug 06, 2011 at 05:37 AM 0
Share

Okay thanks

avatar image Geads · Oct 18, 2017 at 01:51 AM 0
Share

How do I get this same functionality but for mobile buttons?

avatar image
5

Answer by suyujin · Oct 18, 2011 at 09:46 PM

you need to create a "fireSpeed" variable, so var fireSpeed = .5 will make them fire every half a second. then all you need is in your if statement,

if(Input.GetButton("Fire1") && Time.time > firespeed)

what that does is compares the current time passed to your firespeed. I believe that's what you're looking for.

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 DayyanSisson · Oct 19, 2011 at 02:02 AM 0
Share

I'm not sure if you realized, but this was answered already, although that would have been an acceptable answer for one of my other questions (already answered). Anyway, thanks for trying though.

avatar image
2

Answer by tbablani · Dec 30, 2019 at 01:50 PM

create a fire rate then check how much time has pass using

public float firerate = 1f; public float canfire= 1f; if(input.GetButton("Fire1") && Time.time > canfire){ canfire shoot(); canfire = Time.time + firerate;

}

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Firing System with two Shoot Points 1 Answer

how do i stop automatic firing? 3 Answers

Rapid fire script? 4 Answers

Pressing 2 Buttons at the Same Time 2 Answers

How to stop enemies from shooting each other 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