Answer the question
In order to leave comments, you need to log in
Why does the enemy die from a different number of hits?
As part of self-study, I repeated the attack script from one YouTube channel. The script looks like this:
void Update()
{if (timeBtwAttack <= 0)
{
timeBtwAttack = startTimeBtwAttack;
}
else
{
timeBtwAttack -= Time.deltaTime;
}
//нанесение удара
if (Input.GetKeyDown(KeyCode.Space))
{
Attack();
}
}
void Attack()
{
//Воспросизвести анимацию аттаки
anim.SetTrigger("Attack");
//обнаружить всех врагов в зоне действия
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
//Нанести урон
for (int i = 0; i < hitEnemies.Length ; i++)
{
hitEnemies[i].GetComponent<FollowingEnemyHP>().GetDamage(firstAttackDamage);
}
}
private void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowingEnemyHP : MonoBehaviour
{
private float currentHealth;
public float health = 100;
private float damage = 0.1f; // урон наносимый игроку
private void Start()
{
currentHealth = health;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.name == "Player")
{
collision.GetComponent<PlayerHP>().TakeDamage(damage);
}
}
public void GetDamage(float amount)
{
currentHealth -= amount;
if (currentHealth <= amount)
{
Die();
}
void Die()
{
Destroy(gameObject);
}
}
}
Answer the question
In order to leave comments, you need to log in
Your attack code
if (Input.GetKeyDown(KeyCode.Space))
{
Attack();
}
if (timeBtwAttack <= 0)
{
timeBtwAttack = startTimeBtwAttack;
//нанесение удара
if (Input.GetKeyDown(KeyCode.Space))
{
Attack();
}
}
else
{
timeBtwAttack -= Time.deltaTime;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question