Created
February 23, 2016 15:45
-
-
Save fiskefyren/36781777f24b65a13d39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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