Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- public class Weapon : MonoBehaviour
- {
- public Transform firePoint;
- public GameObject bulletPrefab;
- public float fireRate = 0.5f;
- public int magazineSize = 30;
- public float reloadTime = 2f;
- public int damage = 10;
- public ParticleSystem partSystem;
- public int currentAmmo;
- public Animator anim;
- private bool isReloading = false;
- private float nextFireTime = 1f;
- private void Start()
- {
- currentAmmo = magazineSize;
- }
- private void Update()
- {
- if (isReloading)
- return;
- if (Input.GetButton("Fire1"))
- {
- if (currentAmmo > 0 && Time.time >= nextFireTime)
- {
- nextFireTime = Time.time + fireRate;
- Shoot();
- anim.SetTrigger("Shoot");
- }
- else
- {
- StartCoroutine(Reload());
- }
- }
- if (Input.GetButtonDown("Reload") && currentAmmo < magazineSize)
- {
- anim.SetTrigger("Reload");
- StartCoroutine(Reload());
- nextFireTime = Time.time + fireRate;
- }
- }
- private void Shoot()
- {
- // Создаем пулю
- GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
- // Получаем компонент "Target" из префаба пули
- Target target = bullet.GetComponent<Target>();
- // Если у цели есть компонент "Target", наносим урон
- if (target != null)
- {
- target.TakeDamage(damage);
- Debug.Log("Bullet hit target!");
- }
- else
- {
- Debug.Log("Bullet did not hit target. Target component not found.");
- }
- // Уменьшаем количество патронов
- currentAmmo--;
- // Уничтожаем пулю через некоторое время (предотвращение утечек памяти)
- Destroy(bullet, 2f);
- }
- private IEnumerator Reload()
- {
- isReloading = true;
- // Ждем время перезарядки
- yield return new WaitForSeconds(reloadTime);
- // Перезаряжаем оружие
- currentAmmo = magazineSize;
- isReloading = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment