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

Returning initial point

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

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys i create and Enemy AI script and it works fine, (i hv an enemy that is in inicial position nad if i go inside range he start follow me. if i go out of range he stop chase. when is not chasing i use idle animation. wt i want to know is how i can get it to return the inicial position if i get out of chase range and stay their in idle animation?) i try a lot of things but no one work if anyone can help i will appreciate.

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

    doug0102

    Joined:
    Jan 24, 2014
    Posts:
    3
    Log the mob's position when it first detects the player. When the mob is out of range move back to the logged position and then play your idle animation. You can use the same code you used to chase but put the logged position in the LookAt function. Check the distance between the mob and the logged position to know when it reaches its destination.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Ok i try this one and now mob chase me when inside of range and back to is initial position when out o range but now i get a problem... when it is in is initial position is running like crazy arround that initial position over rotating himself... can u tell me why or wt can i do? this is real annoying me for very long time already... Ty for ur time
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mob : MonoBehaviour
    5. {
    6.     public float speed;
    7.     public float range;
    8.    
    9.     public CharacterController controller;
    10.     public Transform player;
    11.     private Fighter opponent;        //copy of fighter script
    12.    
    13.     public LevelSystem playerLevel;
    14.    
    15.     public double impactTime = 0.36;
    16.     private bool impacted;
    17.     public int experience;
    18.    
    19.     public AnimationClip idle;
    20.     public AnimationClip run;
    21.     public AnimationClip die;
    22.     public AnimationClip attackClip;
    23.    
    24.     public int maxHealth;
    25.     public int health;
    26.     public int damage;
    27.    
    28.     private int stunTime;                            //time that enemy will be stunned
    29.  
    30.     float chaseRange = 15.0f;
    31.  
    32.     float distance;
    33.  
    34.     private Vector3 initialPosition;
    35.  
    36.     // Use this for initialization
    37.     void Start ()
    38.     {
    39.         health = maxHealth;
    40.         opponent = player.GetComponent<Fighter>();
    41.  
    42.         initialPosition = transform.position;
    43.  
    44.     }
    45.    
    46.     // Update is called once per frame
    47.     void Update ()
    48.     {
    49.         if (!isDead ())
    50.         {
    51.             if(stunTime<=0)
    52.             {
    53.                 if (!inRange ())
    54.                 {
    55.                     chase ();
    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.     public void getHit(double damage)
    79.     {
    80.         health = health - (int)damage;
    81.        
    82.         if (health < 0)
    83.         {
    84.             health = 0;  
    85.         }
    86.     }
    87.    
    88.     public void getStun(int seconds)
    89.     {
    90.         CancelInvoke ("stunCountDown");
    91.         stunTime = seconds;
    92.         InvokeRepeating ("stunCountDown", 0f, 1f);
    93.     }
    94.    
    95.     void stunCountDown()
    96.     {
    97.         stunTime = stunTime - 1;
    98.        
    99.         if (stunTime == 0)
    100.         {
    101.             CancelInvoke("stunCountDown");  
    102.         }
    103.     }
    104.    
    105.     bool inRange()
    106.     {
    107.         if (Vector3.Distance (transform.position, player.position) < range)
    108.         {
    109.             return true;      
    110.         }
    111.         else
    112.         {
    113.             return false;
    114.         }
    115.     }
    116.    
    117.     void chase()
    118.     {
    119.         distance = Vector3.Distance(player.position, transform.position);
    120.  
    121.         if (distance <= chaseRange)
    122.         {
    123.             transform.LookAt (player.position);
    124.             controller.SimpleMove (transform.forward * speed);
    125.             animation.CrossFade (run.name);
    126.             }
    127.  
    128.         else
    129.         {
    130.                
    131.             transform.LookAt (initialPosition);
    132.             controller.SimpleMove (transform.forward * speed);
    133.             animation.CrossFade (run.name);
    134.         }
    135.     }
    136.  
    137.     void dieMethod()
    138.     {
    139.         animation.Play (die.name);
    140.        
    141.         if (animation [die.name].time > animation [die.name].length * 0.9)
    142.         {
    143.             playerLevel.exp = playerLevel.exp + experience;
    144.             Destroy(gameObject);
    145.         }
    146.     }
    147.    
    148.     bool isDead()
    149.     {
    150.         if (health <= 0)
    151.         {
    152.             return true;  
    153.         }
    154.         else
    155.         {
    156.             return false;  
    157.         }
    158.     }
    159.    
    160.     void OnMouseOver()
    161.     {
    162.         player.GetComponent<Fighter> ().opponent = gameObject;
    163.     }
    164. }
    165.  
    New code: