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 ()
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.
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.
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?
That's a whole new problem. Search through the questions on this site (and on google) for creating a cooldown timer.
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.
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
5 People are following this question.
Pressing 2 Buttons at the Same Time
Rapid fire script?
Firing System with two Shoot Points
How to Sync bullet spawn with shoot animation
Trouble with script to fire a bullet from a handgun