x


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.

more ▼

asked Aug 05, 2011 at 10:08 PM

Overlord gravatar image

Overlord
561 225 177 192

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Aug 05, 2011 at 10:15 PM

Chris D gravatar image

Chris D
2.7k 13 13 36

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?

Aug 06, 2011 at 12:13 AM Overlord

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

Aug 06, 2011 at 12:27 AM Chris D

Okay thanks

Aug 06, 2011 at 05:37 AM Overlord
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 18, 2011 at 09:46 PM

suyujin gravatar image

suyujin
71 6 4 8

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.

Oct 19, 2011 at 02:02 AM Overlord
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1526
x443
x428
x196
x4

asked: Aug 05, 2011 at 10:08 PM

Seen: 5828 times

Last Updated: Oct 19, 2011 at 02:02 AM