Player being damaged on collision

So im working on my enemy ai (script down below) and everything seems to be working fine, only my player is being damaged once he gets close to the enemy rather than once the animation has played.
Ive tried it all and fixed most of it but this one seems to be out of my comprehension habilities.

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.Mathematics;
using UnityEngine;

public class Player : MonoBehaviour
{
    //movement
    [SerializeField]
    private float _playerSpeed = 3.5f;
    [SerializeField]
    private float _jumpheight = 6f;
    private Vector2 _jump;
    private Rigidbody2D _rb;
    private float direction;
    //groundcheck
    [SerializeField]
    private Vector2 _boxSize;
    [SerializeField]
    private float _castDistance;
    [SerializeField]
    private LayerMask _groundLayer;

    //atack
    [SerializeField]
    private float _attackrate=.5f;
    private float _canatack = -1f;
    [SerializeField]
    private Transform _atackPos;
    [SerializeField]
    private float _atackRange;
    [SerializeField]
    private LayerMask _atackable;
    private int _damageDealt = 10;
    
    
    
    //health
    [SerializeField]
    private float _health = 20;
    private float _maxHealth;

    //anims
    private Animator _animator;
    private bool _isGrounded;
    void Start()
    {

        _maxHealth = _health;
        _jump=new Vector2(0,2);
        _rb = GetComponent<Rigidbody2D>();
        _animator = GetComponent<Animator>();   
        if( _rb == null)
        {
            Debug.Log("rb is null in player");
        }
    }
    
    // Update is called once per frame
    void Update()
    {
        _isGrounded = isGrounded();
        Movement();
        if (Input.GetKeyDown(KeyCode.Return)&&Time.time>_canatack)
        {
            StartCoroutine(Atack());
            //Debug.Log("atacked");
        }
        
    }


    void Movement()
    {

        
        float HorizontalInput = Input.GetAxisRaw("Horizontal");
        Vector2 direction = new Vector2 (HorizontalInput, 0);
        transform.Translate(direction *_playerSpeed* Time.deltaTime);
        _animator.SetFloat("xVelocity", math.abs(HorizontalInput)); 
        _animator.SetFloat("yVelocity", _rb.velocity.y);
        if (Input.GetKeyDown(KeyCode.Space)&&isGrounded()==true)
        {
            
            _rb.velocity = _jump * _jumpheight;
            _animator.SetBool("isJumping",!_isGrounded);
            

        }

        if(HorizontalInput < 0)
        {
            transform.localScale = new Vector2(-1, 1);
        }if (HorizontalInput > 0) {
            transform.localScale = new Vector2(1, 1);
        }
    }
    IEnumerator Atack()
    {
        _animator.SetBool("isAtacking", true);
        
        _canatack = Time.time + _attackrate;
        yield return new WaitForSeconds(.25f);
        Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(_atackPos.position, _atackRange, _atackable);
       
        for (int i = 0; i < enemiesToDamage.Length; i++)
            {
                enemiesToDamage[i].GetComponent<Enemy>().enemyDamage(_damageDealt);
            }
        

        
        _animator.SetBool("isAtacking", false);
       
    }

    private bool isGrounded()
    {
        if (Physics2D.BoxCast(transform.position, _boxSize, 0, -transform.up, _castDistance, _groundLayer))
        {
            _animator.SetBool("isJumping", !_isGrounded);
            return true;
            
        }
        else
        {
            _animator.SetBool("isJumping", !_isGrounded);
            return false;
            
        }
    }
    private void regainHealth()
    {
        _health = _maxHealth;
    }

    public void damagePlayer(int damagetaken)
    {
        _health -= damagetaken;
        if (_health <= 0) {
            _animator.SetTrigger("Die");
            Destroy(this.gameObject,.75f);
        }

    }


    
    //collisions
    void OnCollisionEnter2D(Collision2D other)
    {
        
        if (other.gameObject.tag == "Spike")
        {
            Destroy(this.gameObject);   
        }
    }
   

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "HealthKit")
        {
            regainHealth();
            Destroy(other.gameObject);
        }
    }

    

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireCube(transform.position-transform.up*_castDistance,_boxSize);
        Gizmos.DrawWireSphere(_atackPos.position, _atackRange);
    }

}

Ure right, my bad. But i have since fixed it.
I made a hitbox that damages the player and the hit box follows the attack animation.
thank you for passing by though :slight_smile:

1 Like

Was having the same problem.
I’ll give your hit box suggestion a try!