Created
November 17, 2015 19:50
-
-
Save fiskefyren/817bb01f863af7cbed76 to your computer and use it in GitHub Desktop.
Player shooting script
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 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