Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Range problem

Discussion in 'Scripting' started by Paykoman, Nov 6, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    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
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mob : MonoBehaviour
    5. {
    6.     public float speed;
    7.     public float attackRange;
    8.    
    9.     public CharacterController controller;
    10.     public Transform player;
    11.     public LevelSystem playerLevel;
    12.     private Fighter opponent;
    13.    
    14.     public AnimationClip attackClip;
    15.     public AnimationClip run;
    16.     public AnimationClip idle;
    17.     public AnimationClip die;
    18.    
    19.     public double impactTime = 0.36;
    20.    
    21.     public int maxHealth;
    22.     public int  health;
    23.     public int damage;
    24.    
    25.     private bool impacted;
    26.    
    27.     private int stunTime;
    28.    
    29.     // Use this for initialization
    30.     void Start ()
    31.     {
    32.         health = maxHealth;
    33.         opponent = player.GetComponent<Fighter> ();
    34.     }
    35.    
    36.     // Update is called once per frame
    37.     void Update ()
    38.     {
    39.         if (!isDead ())
    40.         {
    41.             if(stunTime<=0)
    42.             {
    43.                 if (!inRange ())
    44.                 {
    45.                     chase ();
    46.                 }
    47.                 else
    48.                 {
    49.                     animation.Play(attackClip.name);
    50.                     attack();
    51.                    
    52.                     if(animation[attackClip.name].time>0.9*animation[attackClip.name].length)
    53.                     {
    54.                         impacted = false;
    55.                     }
    56.                 }
    57.             }
    58.             else
    59.             {
    60.                
    61.             }
    62.         }
    63.         else
    64.         {
    65.             dieMethod();
    66.         }
    67.     }
    68.    
    69.     void attack()
    70.     {
    71.         if (animation [attackClip.name].time > animation [attackClip.name].length * impactTime&&!impacted&&animation[attackClip.name].time<0.9*animation[attackClip.name].length)
    72.         {
    73.             opponent.getHit(damage);
    74.             impacted = true;
    75.         }
    76.     }
    77.    
    78.     bool inRange()
    79.     {
    80.         if(Vector3.Distance(transform.position, player.position)<attackRange)
    81.         {
    82.             return true;
    83.         }
    84.         else
    85.         {
    86.             return false;
    87.         }
    88.     }
    89.    
    90.     public void getHit(double damage)
    91.     {
    92.         health = health - (int)damage;
    93.        
    94.         if(health<0)
    95.         {
    96.             health = 0;
    97.         }
    98.     }
    99.    
    100.     public void getStun(int seconds)
    101.     {
    102.         CancelInvoke("stunCountDown");
    103.         stunTime = seconds;
    104.         InvokeRepeating("stunCountDown", 0f, 1f);
    105.     }
    106.    
    107.     void stunCountDown()
    108.     {
    109.         stunTime = stunTime - 1;
    110.        
    111.         if(stunTime==0)
    112.         {
    113.             CancelInvoke("stunCountDown");
    114.         }
    115.     }
    116.    
    117.    
    118.     void chase()
    119.     {
    120.         transform.LookAt(player.position);
    121.         controller.SimpleMove(transform.forward*speed);
    122.         animation.CrossFade(run.name);
    123.     }
    124.    
    125.     void dieMethod()
    126.     {
    127.         animation.Play (die.name);
    128.        
    129.         if(animation[die.name].time>animation[die.name].length*0.9)
    130.         {
    131.             playerLevel.exp =     playerLevel.exp + 100;
    132.             Destroy(gameObject);
    133.         }
    134.     }
    135.    
    136.     bool isDead()
    137.     {
    138.         if (health <= 0)
    139.         {
    140.             return true;
    141.         }
    142.         else
    143.         {
    144.             return false;
    145.         }
    146.     }
    147.    
    148.     void OnMouseOver()
    149.     {
    150.         player.GetComponent<Fighter>().opponent = gameObject;
    151.     }
    152. }
    153.  
     
  2. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Anyone plz??? I try 2 more diferent ways but keeping the same problems... do what i do... :s
     
  3. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    What's wrong with
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mob : MonoBehaviour
    5. {
    6.     public float speed;
    7.     public float attackRange;
    8.     public float chaseRange;
    9.  
    10.     public CharacterController controller;
    11.     public Transform player;
    12.     public LevelSystem playerLevel;
    13.     private Fighter opponent;
    14.  
    15.     public AnimationClip attackClip;
    16.     public AnimationClip run;
    17.     public AnimationClip idle;
    18.     public AnimationClip die;
    19.  
    20.     public double impactTime = 0.36;
    21.  
    22.     public int maxHealth;
    23.     public int  health;
    24.     public int damage;
    25.  
    26.     private bool impacted;
    27.  
    28.     private int stunTime;
    29.  
    30.     // Use this for initialization
    31.     void Start ()
    32.     {
    33.         health = maxHealth;
    34.         opponent = player.GetComponent<Fighter> ();
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update ()
    39.     {
    40.         if (!isDead ())
    41.         {
    42.             if(stunTime<=0)
    43.             {
    44.                 if (inChaseRange())
    45.                 {
    46.                    if (!inRange ())
    47.                    {
    48.                        chase ();
    49.                    }
    50.                    else
    51.                    {
    52.                        animation.Play(attackClip.name);
    53.                        attack();
    54.  
    55.                        if(animation[attackClip.name].time>0.9*animation[attackClip.name].length)
    56.                        {
    57.                            impacted = false;
    58.                        }
    59.                    }
    60.                 }
    61.                 else
    62.                 {
    63.                    // Set to idle again
    64.                 }
    65.             }
    66.             else
    67.             {
    68.  
    69.             }
    70.         }
    71.         else
    72.         {
    73.             dieMethod();
    74.         }
    75.     }
    76.  
    77.     void attack()
    78.     {
    79.         if (animation [attackClip.name].time > animation [attackClip.name].length * impactTime&&!impacted&&animation[attackClip.name].time<0.9*animation[attackClip.name].length)
    80.         {
    81.             opponent.getHit(damage);
    82.             impacted = true;
    83.         }
    84.     }
    85.  
    86.     bool inRange()
    87.     {
    88.         if(Vector3.Distance(transform.position, player.position)<attackRange)
    89.         {
    90.             return true;
    91.         }
    92.         else
    93.         {
    94.             return false;
    95.         }
    96.     }
    97.    
    98.     bool inChaseRange()
    99.     {
    100.         if(Vector3.Distance(transform.position, player.position)<chaseRange)
    101.         {
    102.             return true;
    103.         }
    104.         else
    105.         {
    106.             return false;
    107.         }
    108.     }
    109.  
    110.  
    111.     public void getHit(double damage)
    112.     {
    113.         health = health - (int)damage;
    114.  
    115.         if(health<0)
    116.         {
    117.             health = 0;
    118.         }
    119.     }
    120.  
    121.     public void getStun(int seconds)
    122.     {
    123.         CancelInvoke("stunCountDown");
    124.         stunTime = seconds;
    125.         InvokeRepeating("stunCountDown", 0f, 1f);
    126.     }
    127.  
    128.     void stunCountDown()
    129.     {
    130.         stunTime = stunTime - 1;
    131.  
    132.         if(stunTime==0)
    133.         {
    134.             CancelInvoke("stunCountDown");
    135.         }
    136.     }
    137.  
    138.  
    139.     void chase()
    140.     {
    141.         transform.LookAt(player.position);
    142.         controller.SimpleMove(transform.forward*speed);
    143.         animation.CrossFade(run.name);
    144.     }
    145.  
    146.     void dieMethod()
    147.     {
    148.         animation.Play (die.name);
    149.  
    150.         if(animation[die.name].time>animation[die.name].length*0.9)
    151.         {
    152.             playerLevel.exp =     playerLevel.exp + 100;
    153.             Destroy(gameObject);
    154.         }
    155.     }
    156.  
    157.     bool isDead()
    158.     {
    159.         if (health <= 0)
    160.         {
    161.             return true;
    162.         }
    163.         else
    164.         {
    165.             return false;
    166.         }
    167.     }
    168.  
    169.     void OnMouseOver()
    170.     {
    171.         player.GetComponent<Fighter>().opponent = gameObject;
    172.     }
    173. }
    174.  
     
    Paykoman likes this.
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    dammm is that so simple.... :p

    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... :) one more week of work probably :D