Hi guys,
Sorry I’ve had to come to the site with this, however I’m at a complete lose. I’ve been working on this(hopefully simple) prb for two days now. I’m sure it’s something I just am not seeing or understanding. Here is my situation.
I have a monster walking around chasing me.
When he gets within a “distance” he begins to attack me. - Here plays attack animation.
When the player gets outside the “distance” range (currently set at 2.5f), the animation SHOULD return back to walk, while continuing to pursue player.
Everything works as it should…except the returning to walk animation when play capsule is outside the range, it just continues to swing at me attacking no matter where the player caps is or how far away it it.
Can someone please tell me the proper way to go about doing this?
Thanks so much for any and all help.
/// EnemyAttack.cs
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;
public GameObject monster;
private bool isAttacking;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
isAttacking = false;
}
// 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");
Debug.Log("I'm attacking you");
}
else
{
animation.CrossFade("2LegsWalk");
Debug.Log("I've stopped attacking you");
}
}
}
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);
if(distance < 2.5f){
isAttacking = true;
}
}
}
}
}