I want this script to not run its Coroutine(Reload) if my other script has set aim to true.
I can get to do that much but then I need it to run after I let go of my aim input which is the right mouse button.
Have tried everything I can think of.
using UnityEngine;
using System.Collections;
public class RayShoot : MonoBehaviour
{
// public GameObject light1;
// public GameObject light2;
// public GameObject light3;
public AudioClip reloadSound;
public AudioClip shootSound;
// public ParticleEmitter muzzleFlash;
public ParticleEmitter hitParticle;
public float damage = 10;
public float range = 1000;
public float reloadTime = 3.3f;
public float shootTimer = 0;
public float shootCooler = 0.9f;
// public float muzzleFlashCooler = 0.5f;
// public float muzzleFlashTimer = 0;
public float keyCooler = 1;
public float keyTimer = 0;
public float force = 1000;
public int clips = 80;
public int bulletsPerClip = 60;
public int bulletsLeft = 0;
public int particleSpeed = 200000;
void Start()
{
bulletsLeft = bulletsPerClip;
hitParticle.emit = false;
// muzzleFlash.emit = false;
}
void Update()
{
if (keyTimer > 0)
{
keyTimer -= Time.deltaTime;
}
if (keyTimer < 0)
{
keyTimer = 0;
}
//
// if (muzzleFlashTimer > 0)
// {
// muzzleFlashTimer -= Time.deltaTime;
// MuzzleFlashShow();
// }
// if (muzzleFlashTimer < 0)
// {
// muzzleFlashTimer = 0;
// }
if (shootTimer > 0)
{
shootTimer -= Time.deltaTime;
}
if (shootTimer < 0)
{
shootTimer = 0;
}
if (keyTimer == 0)
{
if (Input.GetButton("Fire1") && bulletsLeft > 0)
{
if (shootTimer == 0)
{
PlayShotAudio();
RayShooter();
shootTimer = shootCooler;
keyTimer = keyCooler;
}
// if (muzzleFlashTimer == 0)
// {
// muzzleFlashTimer = muzzleFlashCooler;
// MuzzleFlashShow();
// }
}
}
}
// void MuzzleFlashShow()
// {
// if (muzzleFlashTimer > 0)
// {
// muzzleFlash.emit = false;
// light1.SetActive(false);
// light2.SetActive(false);
// light3.SetActive(false);
// }
//
// if (muzzleFlashTimer == muzzleFlashCooler)
// {
// muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * particleSpeed, Vector3.forward);
// muzzleFlash.emit = true;
// light1.SetActive(true);
// light2.SetActive(true);
// light3.SetActive(true);
// }
// }
void RayShooter()
{
//GameObject.Find("m4_fp").animation.Play("fire");
RaycastHit hit;
Vector3 directionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay (transform.position, directionRay * range, Color.green);
if (Physics.Raycast(transform.position, directionRay, out hit, range))
{
if (hit.rigidbody)
{
if (hitParticle)
{
hitParticle.transform.localPosition = hit.point;
hitParticle.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
hitParticle.Emit();
hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
}
bulletsLeft --;
if (bulletsLeft <= 0)
{
bulletsLeft = 0;
}
if (bulletsLeft <= 0)
{
Reload();
clips -= 1;
}
}
void PlayShotAudio()
{
audio.PlayOneShot(shootSound);
}
void PlayReloadAudio()
{
audio.PlayOneShot(reloadSound);
}
void Reload()
{
if (clips <= 0)
{
return;
}
StartCoroutine(ReloadSpeed());
}
IEnumerator ReloadSpeed()
{
GameObject.Find("m4_fp").animation.Play("reload");
PlayReloadAudio();
yield return new WaitForSeconds(reloadTime);
if (clips > 0)
{
bulletsLeft = bulletsPerClip;
}
}
}
And this is my aiming script if it helps.
MuzzleFlash stuff is all commented out because I need to work on the particles and lights for it.
They looked like crap for now so I decdided to wait on it.
using UnityEngine;
using System.Collections;
public class FOVAim : MonoBehaviour
{
public GameObject aimCam;
public float fovAim = 35;
public float defFov;
public float aimSpeed = 5;
public bool aim = false;
void Start()
{
defFov = aimCam.camera.fieldOfView;
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
aim = true;
}
if (Input.GetMouseButtonUp(1))
{
aim = false;
}
if (aim == true)
{
aimCam.camera.fieldOfView = Mathf.Lerp(aimCam.camera.fieldOfView, fovAim, aimSpeed * Time.deltaTime);
}
if (aim == false)
{
aimCam.camera.fieldOfView = Mathf.Lerp(aimCam.camera.fieldOfView, defFov, aimSpeed * Time.deltaTime);
}
}
}
I take no credit for the code in these scripts they are from some tutorials by