Hello there, I have recently been trying to make a game but I need a way for my player to take damage. My enemy can currently follow the player and track its location but when it reaches the player, it cant attack but i can attack the enemy. If anyone has any scripts for player health and enemy attacking that would be great. ill post my playercombat script and my enemy script for if theres a way you can find to implement this into my scripts i already have
Player Combat 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 float attackRate = 1f;
float nextAttackTime = 0f;
// Update is called once per frame
void Update()
{
// Attack
if(Time.time >= nextAttackTime)
{
if (Input.GetButtonDown("Fire1"))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
}
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);
}
}
Enemy Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
//all variables
public int maxHealth = 100;
int currentHealth;
public Animator animator;
public GameObject AITracker;
public string AIDestinationSetter;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
// Enemy Taking Damage
currentHealth -= damage;
// Play Hurt Animation
animator.SetTrigger("Hurt");
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
// Play Death Animation
animator.SetBool("IsDead", true);
// Disable The Enemy
GetComponent<Collider2D>().enabled = false;
(AITracker.GetComponent(AIDestinationSetter) as MonoBehaviour).enabled = false;
this.enabled = false;
}
}
any help would be greatly appreciated. Thank you in advance!