Skip to content

Instantly share code, notes, and snippets.

@fiskefyren
Created November 17, 2015 19:50
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/817bb01f863af7cbed76 to your computer and use it in GitHub Desktop.
Save fiskefyren/817bb01f863af7cbed76 to your computer and use it in GitHub Desktop.
Player shooting script
using UnityEngine;
using System.Collections;
public class PlayerShooting : MonoBehaviour {
public GameObject shot;
public Transform showSpawn;
public float fireRate;
public float cooldown;
//check to see if we're actually firing
public bool isFiring = false;
//firing point transform for launching projectiles
public Transform leftFirePoint;
public Transform rightFirePoint;
public GameObject laserPrefab;
public AudioSource fireFXSound;
void Start () {
isFiring = false;
}//start ends
void Update () {
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