Created
December 19, 2017 07:18
-
-
Save Curookie/9daf1e4e7792ca6800f7170a8038f6c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerATTCtrl : 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