/
KasatkinGV
/
CourseWork
Обзор
Документация
Войти
/
KasatkinGV
/
CourseWork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
DamageZone.cs
73 строки
2 KB
KasatkinGV
upload files
11 июн 2025, 01:06
11 июн 2025, 01:06
3efd0d1
Код
Авторство
О чём код?
using UnityEngine; using System.Collections; public class DamageZone : MonoBehaviour { private int damageAmount = 20; private float damageInterval = 0.5f; private bool playerInside = false; private PlayerHealth playerHealth; private PlayerBlock playerBlock; private Coroutine damageCoroutine; private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { playerInside = true; playerHealth = other.GetComponent<PlayerHealth>(); playerBlock = other.GetComponent<PlayerBlock>(); if (playerBlock != null) { playerBlock.SetInDamageZone(true); } if (damageCoroutine == null && playerHealth != null) { damageCoroutine = StartCoroutine(DamageRoutine()); } } } private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Player")) { playerInside = false; if (playerBlock != null) { playerBlock.SetInDamageZone(false); } if (damageCoroutine != null) { StopCoroutine(damageCoroutine); damageCoroutine = null; } } } private IEnumerator DamageRoutine() { while (playerInside) { playerHealth.TakeDamage(damageAmount); yield return new WaitForSeconds(damageInterval); } damageCoroutine = null; } }