using UnityEngine;
using System.Collections;
public class WeaponShoot : MonoBehaviour {
public PlayerWeapon[] Weapons;
AudioSource audiosource;
private RaycastHit hit;
private bool Firing = false;
private bool Reloading = false;
private PlayerWeapon curWeapon;
public float recoil = 50f;
public AudioClip shoot;
void Awake()
{
ChangeWeaponToSlot (0);
audiosource = GetComponent ();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.R) && !Reloading)
{
StartCoroutine(isReloading());
}
else if (Input.GetButton("Fire1") && !Firing)
{
StartCoroutine(isFiring());
}
}
void shot () {
Vector3 myTransform = transform.transform.forward;
Physics.Raycast (transform.position, myTransform, out hit, 50);
if ((Physics.Raycast (transform.position, myTransform, out hit, 50)) && (hit.collider.gameObject.tag == ("Enemy")))
{
hit.collider.SendMessageUpwards("DoDamage");
Debug.Log ("hit");
}
}
private IEnumerator isFiring()
{
float rateOfFirePerSecond = 1 / (curWeapon.fireRate / 60);
Firing = true;
Camera.main.transform.Rotate( recoil * Time.deltaTime, 0, 0 );
while (Input.GetMouseButton(0) && curWeapon.CurAmmo > 0)
{
Fire();
yield return new WaitForSeconds(rateOfFirePerSecond);
}
Firing = false;
}
private IEnumerator isReloading ()
{
float reloadTimePerBullet = curWeapon.ReloadTime / curWeapon.AmmoMax;
Reloading = true;
while (curWeapon.AmmoMax > 0 && curWeapon.CurAmmo < curWeapon.ClipSize)
{
Reload();
yield return new WaitForSeconds(reloadTimePerBullet);
}
Reloading = false;
}
void Fire()
{
Vector3 forwardLocal = transform.forward;
if ((Physics.Raycast(transform.position, forwardLocal, out hit, 50)) && (hit.collider.gameObject.tag == ("enemy")))
{
hit.collider.GetComponent().DoDamage(curWeapon.Damage);
}
audiosource.PlayOneShot(curWeapon.GunShot, 0.7f);
curWeapon.CurAmmo--;
}
void Reload()
{
curWeapon.AmmoMax--;
curWeapon.CurAmmo++;
}
void ChangeWeaponToSlot(int index)
{
if(Weapons.Length > index)
{
curWeapon = Weapons[index];
}
}
}
whenever i run my game and try to hit an enemy it gives me this error: DivideByZero Expection: Division by zero
↧