using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotgunScript : MonoBehaviour
{
[Header("Setup")]
public CameraScript mainCamera; // For controls
public ChangeVolume volumeSetting;
public AimAndControlsSetting aim;
public Camera fpsCam; // For shooting
public PlayerStats player;
public WeaponSwitch weapons;
public Animator animator;
public Text ammoText;
[Header("General")]
public float range;
public float reloadTime = 3f;
private bool isReloading = false;
private float nextTimeToFire = 0f;
[Header("Bullets")]
public float damage = 50f;
public float fireRate;
public float impactForce = 20f;
public int maxGunAmmoSG = 9;
public int currentGunAmmoSG;
public int totalAmmoSG = 27;
private int amountNeeded;
public int AmountOfProjectiles;
public Vector3 spread;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public GameObject breakEffect;
[Header("Audio")]
public AudioSource reloadGun;
public AudioSource gunFire;
public AudioSource emptyClip;
// Use this for initialization
void Start()
{
volumeSetting = FindObjectOfType<ChangeVolume>();
aim = FindObjectOfType<AimAndControlsSetting>();
currentGunAmmoSG = maxGunAmmoSG;
ammoText.text = currentGunAmmoSG.ToString() + " / " + totalAmmoSG;
}
void OnEnable()
{
isReloading = false;
animator.SetBool("Reloading", false);
}
// Update is called once per frame
void Update()
{
if (isReloading)
{
return;
}
if (currentGunAmmoSG <= 0 && totalAmmoSG > 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetKeyDown(KeyCode.R) && currentGunAmmoSG < maxGunAmmoSG && totalAmmoSG > 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire && currentGunAmmoSG > 0)
{
animator.SetBool("isFiring", true);
nextTimeToFire = Time.time + 1f / fireRate;
for(int i = 0; i < AmountOfProjectiles; i++)
{
Shoot();
}
}
else if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire && currentGunAmmoSG == 0 && totalAmmoSG == 0)
{
emptyClip.Play();
}
if (Input.GetButton("Fire2") && aim.aimAssist == true)
{
mainCamera.speedH = 1;
}
else
{
mainCamera.speedH = 5;
}
if (Input.GetButtonUp("Fire1"))
{
animator.SetBool("isFiring", false);
}
ammoText.text = currentGunAmmoSG.ToString() + " / " + totalAmmoSG;
amountNeeded = maxGunAmmoSG - currentGunAmmoSG;
}
IEnumerator Reload()
{
isReloading = true;
reloadGun.Play();
animator.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTime - 0.25f);
animator.SetBool("Reloading", false);
yield return new WaitForSeconds(0.25f);
if (amountNeeded <= totalAmmoSG)
{
currentGunAmmoSG = maxGunAmmoSG;
totalAmmoSG -= amountNeeded;
}
else if (amountNeeded > totalAmmoSG)
{
currentGunAmmoSG += totalAmmoSG;
totalAmmoSG = 0;
}
//ammoText.text = currentGunAmmoSG.ToString() + " / " + totalAmmoSG;
isReloading = false;
}
void Shoot()
{
Vector3 direction = fpsCam.transform.forward;
spread += fpsCam.transform.up * Random.Range(-1f, 1f);
spread += fpsCam.transform.right * Random.Range(-1f, 1f);
direction += spread.normalized * Random.Range(0f, 0.2f);
muzzleFlash.Play();
gunFire.Play();
currentGunAmmoSG -= 3;
ammoText.text = currentGunAmmoSG.ToString() + " / " + totalAmmoSG;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, direction, out hit, range))
{
EnemyStats enemyTarget = hit.transform.GetComponent<EnemyStats>();
WallBreak wallTarget = hit.transform.GetComponent<WallBreak>();
DroneStats drone = hit.transform.GetComponent<DroneStats>();
DamagedEnemy firstEnemy = hit.transform.GetComponent<DamagedEnemy>();
if (firstEnemy != null)
{
firstEnemy.TakeDamage(damage);
}
if (drone != null)
{
drone.TakeDamage(damage);
}
if (enemyTarget != null)
{
enemyTarget.TakeDamage(damage);
}
if (wallTarget != null)
{
wallTarget.TakeDamage(damage);
if (wallTarget.health <= 0)
{
GameObject explosion = Instantiate(breakEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(explosion, 6f);
}
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(hit.normal * -impactForce);
}
GameObject impactObject = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactObject, 2f);
}
}
}
aye
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.