Created
October 28, 2017 22:05
-
-
Save JonathanYin/9f05780b63760a8c85477820ee5a43bd 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; | |
public class PlayerShooting : MonoBehaviour | |
{ | |
public int damagePerShot = 20; | |
public float timeBetweenBullets = 0.15f; | |
public float range = 100f; | |
float timer; | |
Ray shootRay = new Ray(); | |
RaycastHit shootHit; | |
int shootableMask; | |
ParticleSystem gunParticles; | |
LineRenderer gunLine; | |
AudioSource gunAudio; | |
Light gunLight; | |
float effectsDisplayTime = 0.2f; | |
void Awake () | |
{ | |
shootableMask = LayerMask.GetMask ("Shootable"); | |
gunParticles = GetComponent<ParticleSystem> (); | |
gunLine = GetComponent <LineRenderer> (); | |
gunAudio = GetComponent<AudioSource> (); | |
gunLight = GetComponent<Light> (); | |
} | |
void Update () | |
{ | |
timer += Time.deltaTime; | |
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) | |
{ | |
Shoot (); | |
} | |
if(timer >= timeBetweenBullets * effectsDisplayTime) | |
{ | |
DisableEffects (); | |
} | |
} | |
public void DisableEffects () | |
{ | |
gunLine.enabled = false; | |
gunLight.enabled = false; | |
} | |
void Shoot () | |
{ | |
timer = 0f; | |
gunAudio.Play (); | |
gunLight.enabled = true; | |
gunParticles.Stop (); | |
gunParticles.Play (); | |
gunLine.enabled = true; | |
gunLine.SetPosition (0, transform.position); | |
shootRay.origin = transform.position; | |
shootRay.direction = transform.forward; | |
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask)) | |
{ | |
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> (); | |
if(enemyHealth != null) | |
{ | |
enemyHealth.TakeDamage (damagePerShot, shootHit.point); | |
} | |
gunLine.SetPosition (1, shootHit.point); | |
} | |
else | |
{ | |
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment