Here is the enemy health script with the TakeDamage() function
// Import the necessary Unity libraries.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Define a public class named EnemyHealth that inherits from MonoBehaviour.
public class EnemyHealth : MonoBehaviour
{
// Declare public float variable maxHealth and set its default value to 50.
// This represents the maximum health of the enemy.
public float maxHealth = 5;
// Declare a private float variable currentHealth.
// This represents the current health of the enemy.
private float currentHealth;
// Declare a public float variable knockbackForce and set its default value to 5.
// This represents the force with which the enemy will be knocked back when damaged.
public float knockbackForce = 5;
// Declare a private Rigidbody2D variable rb.
// This will hold a reference to the Rigidbody2D component of the enemy.
private Rigidbody2D rb;
// The Start method is called before the first frame update.
private void Start()
{
// Set the current health of the enemy to its maximum health at the start of the game.
currentHealth = maxHealth;
//assign the rb variable to be the Rigidbody2D component of the slime enemy
rb = GetComponent<Rigidbody2D>();
}
// Declare a public method TakeDamage that takes two parameters: an integer for the damage and a Transform for the player.
public void TakeDamage(int damage, Transform player)
{
Debug.Log("ENEMY IS DAMAGED");
// Reduce the current health of the enemy by the damage amount.
currentHealth -= damage;
// Check if the current health of the enemy has fallen to or below zero.
if (currentHealth <= 0)
{
Debug.Log("Enemy has been killed"); //DEBUGGING
// If the enemy's health is zero or less, destroy the enemy game object.
Destroy(gameObject);
}
Debug.Log("ENEMY IS KNOCKED BACK"); //DEBUGGING
// Calculate the direction in which the enemy should be knocked back.
// This is done by subtracting the player's position from the enemy's position to get the direction from the player to the enemy.
// The result is then normalized to get a unit vector in the desired direction.
Vector2 knockbackDirection = (rb.transform.position - player.position).normalized;
// Apply a force to the enemy's Rigidbody2D in the direction calculated above.
// The force is equal to the knockback direction multiplied by the knockback force.
// The force is applied instantly, simulating the effect of an impulse (hence ForceMode2D.Impulse).
rb.AddForce(knockbackDirection * knockbackForce, ForceMode2D.Impulse);
}
}
Here is the player combat script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHitScript : MonoBehaviour
{
public Transform attackPoint; // The point where the attack will originate.
public LayerMask enemyLayers; // Layer to identify enemies.
public SpriteRenderer playerSprite; // Reference to the player's SpriteRenderer.
public float attackRange = 1f; // Range of the attack.
public int attackDamage = 1; // Damage dealt by the attack.
void Awake()
{
enemyLayers = LayerMask.GetMask("enemy");
}
// Update is called once per frame.
void Update()
{
if (Input.GetMouseButton(0))
{
// Perform the attack.
Attack();
}
// Flip attack point based on player's facing direction.
if (playerSprite.flipX)
{
attackPoint.localPosition = new Vector3(-Mathf.Abs(attackPoint.localPosition.x), attackPoint.localPosition.y, attackPoint.localPosition.z);
}
else
{
attackPoint.localPosition = new Vector3(Mathf.Abs(attackPoint.localPosition.x), attackPoint.localPosition.y, attackPoint.localPosition.z);
}
}
void Attack()
{
Debug.Log("Attack method starting"); //DEBUGGING
// Detects all enemies within the attack range and stores them in an array.
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
// For each enemy that was hit, call the TakeDamage method on their EnemyHealth script.
foreach (Collider2D enemy in hitEnemies)
{
Debug.Log("An enemy(ies) has been damaged");
enemy.GetComponent<EnemyHealth>().TakeDamage(attackDamage, transform);
}
}
// This method is used to visualize the attack range in the Unity editor.
void OnDrawGizmosSelected()
{
// If there is no attack point assigned, exit the method.
if (attackPoint == null)
return;
// Draws a wireframe sphere at the attack point position with a radius equal to the attack range.
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
I am using Unity version 2022.3.20f1. My code editor is VSC.
I wanted the enemies detected from attackPoint in the OverLapCircle() function to be called to the TakeDamage function and decrease their health. I have tried assigning all variables correctly and I still am unable for the OverLapCircle to detect any enemies.