база
This commit is contained in:
1 parent
6e3d5ab9a2
commit
c9085558cd
552 files changed
+133467
-220
No files matched your search
@@ -1,20 +1,34 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class PlayerShooting : MonoBehaviour
|
||||
{
|
||||
// --- Настройки оружия ---
|
||||
public int damagePerShot = 20;
|
||||
public float timeBetweenBullets = 0.15f;
|
||||
public float range = 100f;
|
||||
public float effectsDuration = 0.05f;
|
||||
|
||||
float timer;
|
||||
Ray shootRay = new Ray();
|
||||
RaycastHit shootHit;
|
||||
int shootableMask;
|
||||
ParticleSystem gunParticles;
|
||||
LineRenderer gunLine;
|
||||
AudioSource gunAudio;
|
||||
Light gunLight;
|
||||
float effectsDisplayTime = 0.2f;
|
||||
// --- Боеприпасы и перезарядка ---
|
||||
public int maxAmmo = 30;
|
||||
private int currentAmmo;
|
||||
public float reloadTime = 1.5f;
|
||||
private bool isReloading = false;
|
||||
|
||||
// --- Ссылки на компоненты и объекты ---
|
||||
public GameObject muzzleFlashSprite;
|
||||
public Text ammoText;
|
||||
public AudioClip reloadSound;
|
||||
|
||||
private float timer;
|
||||
private Ray shootRay = new Ray();
|
||||
private RaycastHit shootHit;
|
||||
private int shootableMask;
|
||||
private ParticleSystem gunParticles;
|
||||
private LineRenderer gunLine;
|
||||
private AudioSource gunAudio;
|
||||
private Light gunLight;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
@@ -25,57 +39,117 @@ public class PlayerShooting : MonoBehaviour
|
||||
gunLight = GetComponent<Light>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
currentAmmo = maxAmmo;
|
||||
UpdateAmmoText();
|
||||
|
||||
if (gunLight != null) gunLight.enabled = false;
|
||||
if (muzzleFlashSprite != null) muzzleFlashSprite.SetActive(false);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
timer += Time.deltaTime;
|
||||
|
||||
if (Input.GetButton("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
|
||||
if (Input.GetKeyDown(KeyCode.R) && !isReloading && currentAmmo < maxAmmo)
|
||||
{
|
||||
StartCoroutine(Reload());
|
||||
return;
|
||||
}
|
||||
|
||||
if (isReloading)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Input.GetButton("Fire1") && timer >= timeBetweenBullets && currentAmmo > 0)
|
||||
{
|
||||
Shoot();
|
||||
}
|
||||
|
||||
if (timer >= timeBetweenBullets * effectsDisplayTime)
|
||||
else if (Input.GetButtonDown("Fire1") && currentAmmo <= 0)
|
||||
{
|
||||
DisableEffects();
|
||||
// Звук щелчка пустого магазина можно добавить здесь
|
||||
}
|
||||
}
|
||||
|
||||
public void DisableEffects()
|
||||
{
|
||||
gunLine.enabled = false;
|
||||
gunLight.enabled = false;
|
||||
}
|
||||
|
||||
void Shoot()
|
||||
{
|
||||
timer = 0f;
|
||||
|
||||
gunAudio.Play();
|
||||
currentAmmo--;
|
||||
UpdateAmmoText();
|
||||
|
||||
gunLight.enabled = true;
|
||||
gunAudio.Play();
|
||||
|
||||
gunParticles.Stop();
|
||||
gunParticles.Play();
|
||||
|
||||
gunLine.enabled = true;
|
||||
gunLine.SetPosition(0, transform.position);
|
||||
StartCoroutine(HandleEffects());
|
||||
|
||||
shootRay.origin = transform.position;
|
||||
shootRay.direction = transform.forward;
|
||||
|
||||
if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
|
||||
{
|
||||
// We hit a shootable object
|
||||
Target target = shootHit.collider.GetComponent<Target>();
|
||||
if (target != null)
|
||||
{
|
||||
target.TakeDamage(damagePerShot);
|
||||
}
|
||||
gunLine.SetPosition(0, transform.position);
|
||||
gunLine.SetPosition(1, shootHit.point);
|
||||
}
|
||||
else
|
||||
{
|
||||
gunLine.SetPosition(0, transform.position);
|
||||
gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator HandleEffects()
|
||||
{
|
||||
gunLight.enabled = true;
|
||||
gunLine.enabled = true;
|
||||
if (muzzleFlashSprite != null)
|
||||
{
|
||||
muzzleFlashSprite.SetActive(true);
|
||||
muzzleFlashSprite.transform.localRotation = Quaternion.Euler(0, 0, Random.Range(0, 360));
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(effectsDuration);
|
||||
|
||||
gunLight.enabled = false;
|
||||
gunLine.enabled = false;
|
||||
if (muzzleFlashSprite != null)
|
||||
{
|
||||
muzzleFlashSprite.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator Reload()
|
||||
{
|
||||
isReloading = true;
|
||||
|
||||
if (reloadSound != null)
|
||||
{
|
||||
gunAudio.PlayOneShot(reloadSound);
|
||||
}
|
||||
|
||||
if (ammoText != null) ammoText.text = "Перезарядка...";
|
||||
|
||||
yield return new WaitForSeconds(reloadTime);
|
||||
|
||||
currentAmmo = maxAmmo;
|
||||
UpdateAmmoText();
|
||||
isReloading = false;
|
||||
}
|
||||
|
||||
void UpdateAmmoText()
|
||||
{
|
||||
if (ammoText != null)
|
||||
{
|
||||
ammoText.text = "Патроны: " + currentAmmo + " / " + maxAmmo;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user