- Home /
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.
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.
How do I get this same functionality but for mobile buttons?
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.
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;
}
Your answer
Follow this Question
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