x


Weapon Selector

Alright i have two different weapons at the moment, a single shot, and a triple shot. Now i have the 3-way shot set as a power-up. So if i get to the power-up, i got to shoot 3 bullets at once, over just one. That part works, but I want it so that after i get the power-up, it will automatically make it so i can switch from single shot to triple shot at the push of a button (1 and 2). Here is how i thought about doing it, but it doesn't seem to work.

 function Update () {
if(Input.GetKeyDown("1")) {
var weapon = 1;
}

if (weapon == 1 ) {
if(Input.GetButtonDown("Fire1"))
{

The actual code is quite a bit longer so i only grabbed the parts that pretain to it. Also, the if (weapon == 1) part IS in the update function. Now the actual problem with this script is that when i press 1, it seems it does nothing, but if i repeatedly press 1 and fire at the same time, i will randomly shoot 3 amidst lots of single shots. So more or less i want it so it is a toggle. I want it so when weapon is set to 1 it will stay 1 till i click something different.

Lastly, after the checking what weapon is part i have it making the bullets, adding force, and adding random rotation to make them spread. I also have taken a look at the FPS tutorial but it makes no sense to me how they did it.

more ▼

asked Feb 18 '10 at 12:25 AM

xToxicInferno gravatar image

xToxicInferno
485 24 28 41

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

1 answer: sort newest

You set your weapon variable to 1 by pressing "1" (and set to 2 when pressing 2). So far so good.

Now you could check whether the player is firing any weapon and inside this if-statement you would create the two scenarios for 1-shot vs triple-shot. That's pretty much what you have there (just reversed), but if you keep that clean and simple, it should work.

Something like this would be in your update function alongside the if-statements which select your weapon (which you already have).

if(Input.GetButtonDown("Fire1")) {

    	if (weapon == 1) {
    	  //do weapon 1 stuff
    	}
    	if (weapon == 2) {
    	  //do weapon 2 stuff
    	}

}

You could also combine conditions for your if-statement like such:

if (Input.GetButtonDown("Fire1") && weapon == 1) {
   //do weapon 1 stuff here
}

edit/ It's hard to tell from what your lines of code, but it sounds like you got some if-statements mixed up so that you only shoot 3 bullets when you hit the switch-button and the fire-button at the same time. This should be solved with the above example.

more ▼

answered Feb 18 '10 at 01:32 AM

Sebas gravatar image

Sebas
4.1k 12 18 45

As usual it works perfectly, as usual! I also created a new weapon so it is 1,2,3. I guess i should say firing mode rather then weapon...either way thanks for the help.

Feb 18 '10 at 02:47 AM xToxicInferno
(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:

x222
x169

asked: Feb 18 '10 at 12:25 AM

Seen: 1482 times

Last Updated: Feb 18 '10 at 12:25 AM