Hi guys,
I’m having some trouble with my attack animation script and I was hoping someone could take a look at it and help. I’m simply trying to get a monster to attack me while within a certain range and play his attack animation…(which he does correctly), however I can’t get him to return to his default walk animation when not attacking (player out of range). Any help would be greatly appreciated. Please go easy on me, as I’m still quit new to coding. Thanks all.
/// EnemyAttack.cs
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
public bool isAttacking;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
if(isAttacking)
animation.CrossFade("2LegsClawsAttackR");
else
if(!isAttacking)
animation.CrossFade("2LegsWalk");
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
eh.AddjustCurrentHealth(-10);
isAttacking = true;
}
}
}
}