Skip to content

Instantly share code, notes, and snippets.

@fiskefyren
Created February 23, 2016 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiskefyren/36781777f24b65a13d39 to your computer and use it in GitHub Desktop.
Save fiskefyren/36781777f24b65a13d39 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class PlayerShooting : MonoBehaviour {
public float fireRate;
public float cooldown;//wait amout of time before we can shoot again
//check to see if we're actually firing
public bool isFiring = false;
//firing points transform for launching projectiles
public Transform leftFirePoint;
public Transform rightFirePoint;
//our projectile object
public GameObject laserPrefab;
public AudioSource fireFXSound;
void Start () {
isFiring = false;//we do this just to be on the safe side, so it doesn't shoot when we run the game
}//start ends
void Update () {
CheckInput ();//is calling void CheckInput () {...}
cooldown -= Time.deltaTime;
if(isFiring == true) {
//the player has initiated shooting the laser right now
Fire();
}
}//update ends
void CheckInput () {
if(Input.GetMouseButton(0)) {
isFiring = true;
} else {
isFiring = false;
}
}
void Fire() {
if(cooldown > 0) {
return;//do not fire a thing!
}
//play sound FX when player is firing
if(fireFXSound != null) {
fireFXSound.Play();
}
GameObject.Instantiate (laserPrefab, leftFirePoint.position, leftFirePoint.rotation);
GameObject.Instantiate (laserPrefab, rightFirePoint.position, rightFirePoint.rotation);
cooldown = fireRate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment