Pull out the sword and attack the enemy that close to me and in front of me

I want to add some melee attacking to my game. I know how to use melee attack but when I know who is the enemy and I don’t in front of him.

First thing in melee attacks I need to pull out my sword so I’ll be able to attack. I made an animation for that (grab the sword and pull) but I don’t know how to pull the sword out. If I don’t wrong you need attach the sword to the bone in the hand while the animation is play (correct me if I’m wrong). How I can pull in and out the sword? (The sword is not attach to anything, don’t have rigidbody and I don’t have any code for that).

After I pull my sword out I need to attack. The melee attacks I did before was that only the enemy able to do melee attack (works perfect) but now I want me to do that.

This is the part of the enemy’s melee code:

function Attack ()
 {
     if (canAttack)
	 {
		 if (ableMove)
	     {
	         animation.Play("Stop");
		     yield WaitForSeconds (0.3);
		     attackTest = true;
		     ableMove = false;
	     }
		 
		 if (attackTest)
		 {
		     animation.Play("Hit");
		     CharacterControl.healthBarDisplay -= 0.1;
		     canAttack = false;
		     yield WaitForSeconds (coolDown);
		     canAttack = true;
		 }
	 }
 }

(That code works fine) If I attach that code to my character it will work also but not when I fase to the enemy and when I fight against enemy that not defined as my target. (It works with the enemy because he always look at me and I am he’s only target…). How I can do melee attacks when I fase an enemy? (I don’t care how much enemys, if there is an enemy in front of me and close to me - he loss health). If I don’t wrong you need to use tags (correct me again if it wrong).

Sorry for poor english.

From what I understand, All you need is more animations:

1- Animation for the player standing still

2- Animation for the player pulling the sword

3- Animation for the player standing still WITH the sword

4- Animation for the player attacking with the sword

so when you want to pull the sword out you play the “PULL SWORD” animation
and then change the default animation cycle to “STANDING STILL WITH SWORD”
then whenever you hit you play the “ATTACK WITH SWORD” animation

as for knowing when the enemy is near you, there is a standard used in many games called “Hit Box” , that’s a collider (make it a trigger) attached to the enemy gameobject.

and when your player’s collider enters(or stays inside) the enemy’s collider , you call

var currentEnemy : GameObject;
function OnTriggerEnter(other : collider){


currentEnemy = other.gameObject;

}

and use “currentEnemy” in your code to recieve hits, play “GETTING HIT” animation, and so on

also dont forget to set the “currentEnemy” to null if your player is outside the hitbox of the enemy so that you cant attack the enemy from far away.