x


Firing System with two Shoot Points

I'm making a space sim, and want a script where I could determine the shoot points in the inspector. It ALMOST works. Here's the firing script:

var canShoot : boolean = true;
var projectile : GameObject;
var secondary : GameObject;
var fireRate : float = 0.1;
var secondaryFireRate : float = 3.0;
private var nextFire : float = 0.0;
private var nextSecondaryFire : float = 0.0;
var hasMuzzleFlash : boolean = true;
var muzzleFlash : GameObject;
var secondaryMuzzleFlash : GameObject;
static var bulletsLeft : int = 100000000; ///Infinte = 100000000
static var missilesLeft : int = 30;
private var hasFired : boolean = false;
private var hasFiredSecondary : boolean = false;
private var bulletsPerShot : float = 1;
private var missilesPerShot : float = 1;
private var boosting : boolean = false;
var shootPointOne : Transform;
var shootPointTwo : Transform;

function Update() 
{

 hasFired = false;
    hasFiredSecondary = false;

    if(bulletsLeft == 0)
    bulletsPerShot = 0;

    if(missilesLeft == 0)
    missilesPerShot = 0;

    if(Input.GetKey("space")){

    boosting = true;
    }
    else if(fighter){

    boosting = false;
    }


    if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0 && !boosting && canShoot) {
        nextFire = Time.time + fireRate;
        Instantiate (projectile, shootPointOne.position, transform.rotation);
        hasFired = true;
    }


    if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0 && !boosting && canShoot) {

        nextFire = Time.time + fireRate;
        Instantiate (muzzleFlash, shootPointOne.position, transform.rotation); 
}

    if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0 && !boosting && canShoot) {
        nextFire = Time.time + fireRate;
        Instantiate (projectile, shootPointTwo.position, transform.rotation);
        hasFired = true;
    }


    if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0 && !boosting && canShoot) {

        nextFire = Time.time + fireRate;
        Instantiate (muzzleFlash, shootPointTwo.position, transform.rotation); 
}

It pretty much works, and it shoots in the shoot point. But the second shoot point doesn't work. It shoots at shootPointOne, but it doesn't shoot at shootPointTwo. I don't know why it's not working.

more ▼

asked Oct 11 '11 at 03:43 AM

Overlord gravatar image

Overlord
484 67 78 88

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

1 answer: sort oldest

See all those if statements? Put all of that in one if statement. Then, it won't block the ones at the end!

more ▼

answered Oct 11 '11 at 04:18 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

It works, but the problem is, whenever I shoot, the starship starts jerking back and forth. Do you know why it's doing this. There is no rigidbody attached, and even when there was it still did this. It's not the shootPoints or the lasers because even after I took out the shoot points, and it didn't shoot anything, then ship jerked back and forth.

Oct 13 '11 at 11:04 PM Overlord

I only mean the ones to do with shooting. The boost one and all that still have to be separate!

Oct 14 '11 at 03:50 AM syclamoth

I know that. The boost is in it's own section of if statements, and the shooting is in it's own section. Here's the shooting part now:

if(Input.GetButton("Fire1") && Time.time > nextFire && bulletsLeft > 0 && !boosting && canShoot) {
        nextFire = Time.time + fireRate;
        Instantiate (primary, shootPointOne.position, transform.rotation);
        Instantiate (primary, shootPointTwo.position, transform.rotation);
        Instantiate (primary, shootPointThree.position, transform.rotation);
        Instantiate (primary, shootPointFour.position, transform.rotation);
        Instantiate (muzzleFlash, shootPointOne.position, transform.rotation);
        Instantiate (muzzleFlash, shootPointTwo.position, transform.rotation);
        Instantiate (muzzleFlash, shootPointThree.position, transform.rotation);
        Instantiate (muzzleFlash, shootPointFour.position, transform.rotation);
        hasFired = true;
    }

    if(hasFired) {
        bulletsLeft -= bulletsPerShot;
}

    if(Input.GetButton("Fire2") && Time.time > nextSecondaryFire && missilesLeft > 0 && !boosting && canShoot) {
        nextSecondaryFire = Time.time + secondaryFireRate;
        Instantiate (secondary, missileShootPoint.position, transform.rotation);
        Instantiate (secondary, missileShootPointTwo.position, transform.rotation);
        Instantiate (secondaryMuzzleFlash, missileShootPoint.position, transform.rotation);
        Instantiate (secondaryMuzzleFlash, missileShootPointTwo.position, transform.rotation);  
        hasFiredSecondary = true;
    }   
    if(hasFiredSecondary) {
        missilesLeft -= missilesPerShot;
}

See. It's in it's own if statement, away from the rest. I still have no idea why it's jerking.

Oct 14 '11 at 04:43 AM Overlord

Nevermind, it doesn't do that in the build. Thanks.

Oct 31 '11 at 12:29 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:

x329
x275
x152
x109
x55

asked: Oct 11 '11 at 03:43 AM

Seen: 619 times

Last Updated: Oct 31 '11 at 12:29 AM