How do i script basic attack animation?

Hello, everyone, I’m currently having problems trying to add an animation to my attack script. The attack works and the dummy I have in unity loses damage and is destroyed when Health goes to zero but when I try to add the animation I made for the weapon I have it does not respond and I have no idea how to make it work I’m very new to scripting aspect of unity. And I’m very proud of what I’ve done so far, but I’ve been stuck on this problem for hours and do not know how to get around it any help would be greatly appreciated thank you.

When you want to damage an AI with a Sword something like that you can basically apply this method to yours.

First put a collider on the tip of your sword or on the whole sword then add a nametag to your AI something like AI tag and add this script to the sword .

OnTriggerEnter is called when the Collider other enters the trigger.

void OnTriggerEnter(Collider other) 
   {
        //Your function for the health
        healthminusminus();
    }

void OnTriggerExit(Collider other)
    {
        //Do nothing
    }

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class meleesys : MonoBehaviour {
public int Damage = 50;

public float Distance;

public float maxDistance = 1.5f;

public Transform Maice;

RaycastHit hit;

void Update () {

    if (Input.GetButtonDown("Fire1"))

    {
        Maice.animation.Play("attack");

        if (Physics.Raycast(transform.position,
            
            transform.TransformDirection(Vector3.forward), out hit)) {
            Distance = hit.distance;
            if(Distance < maxDistance)
            hit.transform.SendMessage("ApplyDamage", Damage,
                SendMessageOptions.DontRequireReceiver);

        }

    }
	
}

}

well in case you use the legacy animation system, take a look here.

Animation m_anim;
void Start(){
    m_anim = GetComponent<Animation>();
}

Then in you update function, add the code below. I think it’s will work fine.

if (Physics.Raycast(transform.position,
             transform.TransformDirection(Vector3.forward), out hit)) {
             Distance = hit.distance;
             if(Distance < maxDistance){
                 // Add this code:
                 m_anim.Play("<your weapon animation name>");
                 hit.transform.SendMessage("ApplyDamage", Damage,
                     SendMessageOptions.DontRequireReceiver);
             }
}