First of all I’m a newbie so I assume I’m doing a lot of wrong things, but I’ve been serching in this forum and nothing works for me. I’ll try to provide as much as information I can so sorry if this is too long, but Im sutck with this. It seems it does not detect the enemy collider, but its set on the right Layer and with a collider set to trigger. I put the debugs to see where it fails and its on the Overlap detection.
This is my player melee attack script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMeleeCombat : MonoBehaviour
{
public Transform attackPoint; // Reference to the melee attack point
public float attackRange = 0.5f; // Range of the melee attack
public LayerMask enemyLayer; // Layer mask to identify enemy objects
public int attackDamage = 10; // Damage amount of the melee attack
private Animator m_animator;
private int m_currentAttack = 0;
private float m_timeSinceAttack = 0.0f;
private void Start()
{
m_animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
PerformMeleeAttack();
}
}
void PerformMeleeAttack()
{
if (m_timeSinceAttack > 0.25f)
{
m_currentAttack++;
// Loop back to one after third attack
if (m_currentAttack > 3)
m_currentAttack = 1;
// Reset Attack combo if time since last attack is too large
if (m_timeSinceAttack > 1.0f)
m_currentAttack = 1;
// Call one of three attack animations "Attack1", "Attack2", "Attack3"
m_animator.SetTrigger("Attack" + m_currentAttack);
// Detect enemies within the attack range
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayer);
Debug.Log("Number of hit enemies: " + hitEnemies.Length);
// Deal damage to each enemy
foreach (Collider2D enemyCollider in hitEnemies)
{
Debug.Log("Hit");
// Retrieve the EnemyFollow script from the enemy object
EnemyFollow enemy = enemyCollider.GetComponent<EnemyFollow>();
if (enemy != null)
{
enemy.TakeDamage(attackDamage);
}
}
// Reset timer
m_timeSinceAttack = 0.0f;
}
}
// Visualize the attack range in the editor
void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
This is my enemy prefab
With the Gizmo I can see that my range is actually in the right position.
If you think I should give any more info tell me. And sorry for my english is not my first language.
Thank you guys.