Hi again guys, so right now im having a little issue, i hv to scripts the BaseCharacter one and the MeleeEnemy that inherites from Enemy that inherits from BaseCharacter, in the BaseCharacter i define a bool called isMoving and in the MeleeEnemy Start function i set it to false, when enemy start moving it turn true (i can check in inspector) and if i go out of range it goes false, but when is in attack range i define it to go false and it dont returns to false, so this way enemy is attacking me but always move arround player instead of be stopeed (and in enemy attack method i call isMoving = false). Can anyone help me plz?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public abstract class BaseCharacter : MonoBehaviour {
public string characterName;
public float attackRange;
public float attackSpeed;
public float maxHealth;
public float curHealth;
public int damage;
public bool isMoving;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public void GetDamage(int attackDamage)
{
curHealth = curHealth - attackDamage;
if (curHealth <= 0) {
curHealth = 0;
Destroy(gameObject, 0.5f);
}
}
protected abstract void Attack();
}
using UnityEngine;
using System.Collections;
public class MeleeEnemy : Enemy
{
Player player;
bool blockEnemy;
// Use this for initialization
void Start ()
{
curHealth = maxHealth;
player = Player.player;
blockEnemy = false;
isMoving = false;
}
// Update is called once per frame
void Update ()
{
Attack();
FollowPlayer();
}
void FollowPlayer()
{
if(IsInRange(chasingRange))
{
isMoving = true;
navAgent.SetDestination(player.transform.position);
navAgent.Resume();
}
else if (!IsInRange(chasingRange))
{
isMoving = false;
navAgent.SetDestination(transform.position);
}
}
void OnMouseOver()
{
Player.opponent = transform;
}
protected override void Attack()
{
if (IsInRange (attackRange))
{
if(!blockEnemy)
{
if(player.curHealth > 0)
{
isMoving = false;
transform.LookAt(opponent);
player.GetDamage(damage);
blockEnemy = true;
Debug.Log("Player Damaged!");
Invoke("UnBlock", attackSpeed);
}
}
}
}
void UnBlock()
{
blockEnemy = false;
}
bool IsInRange(float range)
{
if (player != null)
{
if (Vector3.Distance (player.transform.position, transform.position) < range)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}