- Home /
How do I fix an issue with my enemy instantly dying?
So I followed a tutorial on youtube, learned what the code meant and altered it slightly to what I needed. It was a tutorial on how to make unity detect clicking and holding. Everything works fine except for the fact that if I release my mouse button when the enemy is colliding with me (I have colliders off for between enemies and players so I guess it's just when the enemy is over the player) the enemy will die. It doesnt matter if hes been previously hit or not. I'd just like to know if anybody may have a solution
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
//all variables
public Animator animator;
public Transform attackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public int attackDamage = 20;
public int powerAttackDamage = 40;
public float nextAttackTime;
float startTime, endTime;
// Update is called once per frame
void Update()
{
// Attack
if(Time.time >= nextAttackTime)
{
if (Input.GetButtonDown("Fire1"))
{
Attack();
nextAttackTime = Time.time + 1f;
}
}
if(Input.GetButtonDown("Fire1"))
{
startTime = Time.time;
}
if(Input.GetButtonUp("Fire1"))
{
endTime = Time.time;
}
if(endTime - startTime > 0.5f)
{
if (Time.time >= nextAttackTime)
{
Attack();
}
}
}
void Attack()
{
//Play Attack Animation
animator.SetTrigger("Attack");
//Detect Enemies In Range Of Attack
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
// Damage The Enemy
foreach(Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
void OnDrawGizmosSelected()
{
if(attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
I believe you may only need to look at the "Attack" void and the code that says:
if(Input.GetButtonDown("Fire1"))
{
startTime = Time.time;
}
if(Input.GetButtonUp("Fire1"))
{
endTime = Time.time;
}
if(endTime - startTime > 0.5f)
{
if (Time.time >= nextAttackTime)
{
Attack();
}
}
Any help would be greatly appreciated! Thank you!
Answer by kolalex01 · Oct 01, 2021 at 12:06 PM
Hi, @ReferenceWolf!
Did you check the health of your enemy? It may happen, that you forgot to set his health in the editor - or in the script - or just set it to less than the character's damage and when you damage it once, it dies intantly.
Hey there @kolalex01, I made sure my enemy's health is 100, as set in my code. I have no idea what it could be. Thank you for the suggestion though!