- Home /
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.
Answer by syclamoth · Oct 11, 2011 at 04:18 AM
See all those if statements? Put all of that in one if statement. Then, it won't block the ones at the end!
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.
I only mean the ones to do with shooting. The boost one and all that still have to be separate!
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 (secondary$$anonymous$$uzzleFlash, missileShootPoint.position, transform.rotation);
Instantiate (secondary$$anonymous$$uzzleFlash, 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.
Never$$anonymous$$d, it doesn't do that in the build. Thanks.
Your answer
Follow this Question
Related Questions
how do i stop automatic firing? 3 Answers
Problem with firing laser in direction of camera 1 Answer
Hold Button Shooting 3 Answers
Audio when I shoot 3 Answers