Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.