// Use this for initialization void Start() { shootableMask = LayerMask.GetMask("Shootable"); laserState = LASERSTATE.OFF; //charge time is equal to the length of the audio clip - dont think there is a way to make it dynamic laserChargeTimeHolder = clip_laserCharge.length; particleBackstop = GameObject.FindGameObjectWithTag("ParticleBackstop").transform; laserFire = transform.FindChild("LaserFire").gameObject; laserLine = laserFire.GetComponent <LineRenderer>(); myAudioSource = GetComponent <AudioSource>(); laserChargeVisual = transform.FindChild("LaserChargeVisual").GetComponent <ParticleSystem>(); laserChargeVisual.Stop(); laserFire.SetActive(false); laserChargeTimer = 0; }
// Update is called once per frame void Update() { //firebutton hit if (Input.GetButton("Fire1")) { switch (laserState) { case LASERSTATE.OFF: laserState = LASERSTATE.CHARGING; break; case LASERSTATE.CHARGING: laserChargeTimer += Time.deltaTime; myAudioSource.clip = clip_laserCharge; if (!(laserChargeVisual.isPlaying)) { laserChargeVisual.Play(); } if (!(myAudioSource.isPlaying)) { myAudioSource.Play(); } if (laserChargeTimer >= laserChargeTimeHolder) { laserState = LASERSTATE.FIRING; } break; case LASERSTATE.FIRING: LaserShoot(); laserChargeVisual.Stop(); if (!laserFire.activeSelf) { laserFire.SetActive(true); } break; } } //fire button not hit if (!Input.GetButton("Fire1")) { switch (laserState) { case LASERSTATE.CHARGING: case LASERSTATE.COOLDOWN: laserChargeVisual.Stop(); myAudioSource.clip = clip_laserChargeReverse; if (laserChargeTimer > 0) { if (!(myAudioSource.isPlaying)) { myAudioSource.Play(); } } laserChargeTimer -= Time.deltaTime; if (laserChargeTimer <= 0) { laserState = LASERSTATE.OFF; } break; case LASERSTATE.FIRING: laserFire.SetActive(false); LaserSmokeDislay(); laserState = LASERSTATE.COOLDOWN; break; } } slider_ChargeDisplay.value = 100 * (1 - (laserChargeTimeHolder - laserChargeTimer) / laserChargeTimeHolder); laserChargeTimer = Mathf.Clamp(laserChargeTimer, 0, laserChargeTimeHolder); }