Тима Мелешкин
Ученик
(89),
на голосовании
3 месяца назад
У меня в моём коде противника на Unity не ждет карутина что делать? Вот код using System.Collections; using System.Collections.Generic; using System.Threading; using Unity.VisualScripting; using UnityEngine;
public class EnemyG : MonoBehaviour { public LayerMask enemyLayers; public Transform attackPoint; public float AttackRange = 0.5f; public int maxHealth = 60; public int currentHealth; [SerializeField] private float speed = 3f; public Transform player; public int attackDamage = 5; [SerializeField] bool PlayerG = false; public float sss = 0.5f; string pl = "Player";
using System.Collections.Generic;
using System.Threading;
using Unity.VisualScripting;
using UnityEngine;
public class EnemyG : MonoBehaviour
{
public LayerMask enemyLayers;
public Transform attackPoint;
public float AttackRange = 0.5f;
public int maxHealth = 60;
public int currentHealth;
[SerializeField] private float speed = 3f;
public Transform player;
public int attackDamage = 5;
[SerializeField] bool PlayerG = false;
public float sss = 0.5f;
string pl = "Player";
public Animator animator;
private void Start()
{
currentHealth = maxHealth;
}
private void Update()
{
if (PlayerG == true)
{
StartCoroutine(Abob());
}
if (currentHealth == 900)
{
attackDamage = 0;
}
if (currentHealth == 800)
{
attackDamage = 20;
}
if (currentHealth == 500)
{
attackDamage = 0;
}
if (currentHealth == 100)
{
attackDamage = 20;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == pl)
{
PlayerG = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == pl)
{
PlayerG = false;
}
}
void Attack()
{
animator.SetTrigger("Attack");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, AttackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<PlayerCombat>().TakeDamageg(attackDamage);
}
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
animator.SetTrigger("Hurt");
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
GetComponent<Collider2D>().enabled = false;
Debug.Log("Enemy die");
animator.SetBool("isDie", true);
Destroy(gameObject, 2f);
}
private void FixedUpdate()
{
transform.position = Vector2.MoveTowards(current: transform.position, player.position, speed * Time.fixedDeltaTime);
}
private void OnDrawGizmosSelected()
{
if (attackPoint == null)
{
return;
}
Gizmos.DrawWireSphere(attackPoint.position, AttackRange);
}
private IEnumerator Abob()
{
yield return new WaitForSeconds(sss);
Attack();
}
}