Attack Animation problem

Alright so i’ve written the code below and what i want is if the player skill is 1 then the enemy plays the attack animation. The problem is when he tries to play the attack animation it freezes at the first frame. Now i tried stopping the idle animation and then play the attack animation but nothing. I used a coroutine as well but still nothing. Any other suggestions? Notice that this is just the update function of my code :slight_smile: Thanks!

void Update () {
		if (Vector3.Distance (transform.position, player.transform.position) > 2) {
			chase ();
		} else {
			battle_mode_on = true;
			GetComponent<Animation>().Play (idle.name);
			player.GetComponent<Battle_Mode_Player> ().enemy_target = gameObject;
			healthbar = true;
		}
		if (health <= 0) {
			Destroy (gameObject);
			player.GetComponent<Battle_Mode_Player> ().enemy_target = null;
			player.GetComponent<PlayerStats> ().curxp += 30;
			healthbar = false;
		}
		if (battle_mode_on) {
			player_skill = player.GetComponent<Battle_Mode_Player>().skill_used;
			if(player_skill == 1){
				GetComponent<Animation>().Play (attack.name);
			}
		}
	}

Just a suggestion, give a second review to your logic. At some point, 3 conditions may be met within the a single game cycle, causing switching animations from idle to attack over and over

Suppose that

if (Vector3.Distance (transform.position, player.transform.position) > 2)

And

if (battle_mode_on)

And

if(player_skill == 1)

are all met. Then within the same game cycle your code will go from the “idle” animation to the “attack” animation within a single game cycle. Another thing to keep in mind is that you’re running this on an update, so if the conditions are met on consecutive game cycles, the Animation.Play is fired on each cycle, causing it to play over and over from the beginning. If your game logic will never allow this, then you’re good here.

Last logic point is to check if the requested animation is not playing before trying to start it back. If your animation is already a loop, that scenario will reset the animation to the initial frame over an over. If it’s a loop and it’s already playing, you don’t need to invoke the play event anymore.

Here’s a link that could help you with the last part:

Regards