Range problem

Hi guys i hv my enemy ai script working 100%. after game start enemie start chase me because is out od attack range (attackRange = 1.25f). So wt i want to do is instead of always chase me i want it to start in idle and just chase when im inside a lookRange like something about 15.0f and if i get auto of that 15.0f it stop follow me and stay idle again.

i try lots of things but everytime i do the right thing about the chase method i get something to stop, like enemy stop attack me, or attack but only discount the 1st hit in my health…

If someone can help me implemente this correctly i will appreciate it a lot. There is the actual script.

Ty

using UnityEngine;
using System.Collections;

public class Mob : MonoBehaviour
{
    public float speed;
    public float attackRange;
   
    public CharacterController controller;
    public Transform player;
    public LevelSystem playerLevel;
    private Fighter opponent;
   
    public AnimationClip attackClip;
    public AnimationClip run;
    public AnimationClip idle;
    public AnimationClip die;
   
    public double impactTime = 0.36;
   
    public int maxHealth;
    public int  health;
    public int damage;
   
    private bool impacted;
   
    private int stunTime;
   
    // Use this for initialization
    void Start ()
    {
        health = maxHealth;
        opponent = player.GetComponent<Fighter> ();
    }
   
    // Update is called once per frame
    void Update ()
    {
        if (!isDead ())
        {
            if(stunTime<=0)
            {
                if (!inRange ())
                {
                    chase ();
                }
                else
                {
                    animation.Play(attackClip.name);
                    attack();
                   
                    if(animation[attackClip.name].time>0.9*animation[attackClip.name].length)
                    {
                        impacted = false;
                    }
                }
            }
            else
            {
               
            }
        }
        else
        {
            dieMethod();
        }
    }
   
    void attack()
    {
        if (animation [attackClip.name].time > animation [attackClip.name].length * impactTime&&!impacted&&animation[attackClip.name].time<0.9*animation[attackClip.name].length)
        {
            opponent.getHit(damage);
            impacted = true;
        }
    }
   
    bool inRange()
    {
        if(Vector3.Distance(transform.position, player.position)<attackRange)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
    public void getHit(double damage)
    {
        health = health - (int)damage;
       
        if(health<0)
        {
            health = 0;
        }
    }
   
    public void getStun(int seconds)
    {
        CancelInvoke("stunCountDown");
        stunTime = seconds;
        InvokeRepeating("stunCountDown", 0f, 1f);
    }
   
    void stunCountDown()
    {
        stunTime = stunTime - 1;
       
        if(stunTime==0)
        {
            CancelInvoke("stunCountDown");
        }
    }
   
   
    void chase()
    {
        transform.LookAt(player.position);
        controller.SimpleMove(transform.forward*speed);
        animation.CrossFade(run.name);
    }
   
    void dieMethod()
    {
        animation.Play (die.name);
       
        if(animation[die.name].time>animation[die.name].length*0.9)
        {
            playerLevel.exp =     playerLevel.exp + 100;
            Destroy(gameObject);
        }
    }
   
    bool isDead()
    {
        if (health <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
    void OnMouseOver()
    {
        player.GetComponent<Fighter>().opponent = gameObject;
    }
}

Anyone plz??? I try 2 more diferent ways but keeping the same problems… do what i do… :s

What’s wrong with

using UnityEngine;
using System.Collections;

public class Mob : MonoBehaviour
{
    public float speed;
    public float attackRange;
    public float chaseRange;

    public CharacterController controller;
    public Transform player;
    public LevelSystem playerLevel;
    private Fighter opponent;

    public AnimationClip attackClip;
    public AnimationClip run;
    public AnimationClip idle;
    public AnimationClip die;

    public double impactTime = 0.36;

    public int maxHealth;
    public int  health;
    public int damage;

    private bool impacted;

    private int stunTime;

    // Use this for initialization
    void Start ()
    {
        health = maxHealth;
        opponent = player.GetComponent<Fighter> ();
    }

    // Update is called once per frame
    void Update ()
    {
        if (!isDead ())
        {
            if(stunTime<=0)
            {
                if (inChaseRange())
                {
                   if (!inRange ())
                   {
                       chase ();
                   }
                   else
                   {
                       animation.Play(attackClip.name);
                       attack();

                       if(animation[attackClip.name].time>0.9*animation[attackClip.name].length)
                       {
                           impacted = false;
                       }
                   }
                }
                else
                {
                   // Set to idle again
                }
            }
            else
            {

            }
        }
        else
        {
            dieMethod();
        }
    }

    void attack()
    {
        if (animation [attackClip.name].time > animation [attackClip.name].length * impactTime&&!impacted&&animation[attackClip.name].time<0.9*animation[attackClip.name].length)
        {
            opponent.getHit(damage);
            impacted = true;
        }
    }

    bool inRange()
    {
        if(Vector3.Distance(transform.position, player.position)<attackRange)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
    bool inChaseRange()
    {
        if(Vector3.Distance(transform.position, player.position)<chaseRange)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public void getHit(double damage)
    {
        health = health - (int)damage;

        if(health<0)
        {
            health = 0;
        }
    }

    public void getStun(int seconds)
    {
        CancelInvoke("stunCountDown");
        stunTime = seconds;
        InvokeRepeating("stunCountDown", 0f, 1f);
    }

    void stunCountDown()
    {
        stunTime = stunTime - 1;

        if(stunTime==0)
        {
            CancelInvoke("stunCountDown");
        }
    }


    void chase()
    {
        transform.LookAt(player.position);
        controller.SimpleMove(transform.forward*speed);
        animation.CrossFade(run.name);
    }

    void dieMethod()
    {
        animation.Play (die.name);

        if(animation[die.name].time>animation[die.name].length*0.9)
        {
            playerLevel.exp =     playerLevel.exp + 100;
            Destroy(gameObject);
        }
    }

    bool isDead()
    {
        if (health <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    void OnMouseOver()
    {
        player.GetComponent<Fighter>().opponent = gameObject;
    }
}
1 Like

dammm is that so simple… :stuck_out_tongue:

really appreciate ur help m8 O now just hv to work in one more thing… return enemy to it initial position if i go out of chaserange or if i die… :slight_smile: one more week of work probably :smile: