Unity Monster AI

Source: Internet
Author: User

Using Unityengine; Using System.Collections;

public class Moster_ai:monobehaviour {

public const int ai_null = 10;     public const int ai_idle = 0;     public const int Ai_patrol = 1;     public const int ai_pursuit = 2;     public const int ai_attack = 3;    public const int ai_attack_idle = 4; public const int aia_backpoint = 5;

public const int Dead = 6;

Public Const FLOAT followupdistance = 3.0f;    Public Const FLOAT attackdistance = 2.0f;    Public Const FLOAT Vattackdeltatime = 1.0f; Public Const FLOAT Maxfollow = 20.0f;

Public Const FLOAT speed = 2.0f;     Public Const FLOAT Runspeed = 3.0f;   Public Const FLOAT Vescapespeed = 6.0f;   Public rect vrect = new rect (0,0,10,10);    Public Vector2 Dis;  Public Vector2 Srcpoint;     Birth coordinates public gameobject player;    public int vstartstate; public int vloststate;

 //use initialization    void Start () {        & Nbsp;vstartstate = ai_idle;       vloststate = ai_null;        srcpoint = new Vector2 (transform.position.x,transform.position.z);        player = Gameobject.find ("fighter");       }                  //Update is called once per frame    

 bool Living;  void Update () {         switch (vstartstate)          {      case ai_idle:                 if (vstartstate! = vloststate)          {                        animation. Crossfade ("Idel");        vloststate = vstartstate;            debug.log ("1");                }    fgetpatrolposition ();               vstartstate = ai_patrol;             break;            case ai_patrol:             if (vstartstate! = vloststate)        {                        Animation. Crossfade ("Run");         vloststate = vstartstate;                  debug.log ("2");                }        fpatrovrect ();                 if (Vector2.distance (dis,new Vector2 (transform.position.x,transform.position.z)) <0.4f)       {                    &NBSP;   //debug.log ("Not in?") ");                      vstartstate = ai_idle;                  }          if (Vector3.distance ( Transform.position, Player.transform.position) < followupdistance)    {                    vstartstate = ai_pursuit;        }                               break;              case ai_pursuit:      &NBSP;&NBSP;&NBSP;IF (vstartstate! = vloststate)       {                       animation. Crossfade ("Attack02");         vloststate = vstartstate;            debug.log ("3");                }    fpursuithero ();                if (Vector3.distance (New Vector3 (Srcpoint.x, TRANSFORM.POSITION.Y, Srcpoint.y), transform.position) > Maxfollow && vector3.distance (transform.position , player.transform.position) > Followupdistance)                 {                Vstartstate = aia_backpoint;     &NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;          if (Vector3.distance (Transform.position, Player.transform.position) < attackdistance)            {                      vstartstate = ai_attack;      }               break;            case Ai_ attack:          if (vstartstate! = vloststate)        {                    vloststate = vstartstate;       debug.log ("4");                 }                   fattackhero ();                if (time.time-backuptime >= 0.2f)      {                        backuptime = time.time;       vstartstate = AI_Attack_Idle;        }                break;          case ai_attack_idle:       if (vstartstate! = vloststate)      {                      animation. Crossfade ("Idel");              vloststate = Vstartstate;            }                 vstartstate = ai_pursuit;       break;             case aia_backpoint:        if (vstartstate! = vloststate)      {                     animation. Crossfade ("Run");         vloststate = vstartstate;          }                 fbackpoint ();          if (Vector2.Distance (SrcPoint, New Vector2 (transform.position.x, transform.position.z) < 1)       {                     vstartstate = AI_Idle;       }                                  break;         Case dead:  //Death status    //if (living==true)    // {    //Living=false;     //gameobject.setactive (false);    animation. Crossfade ("Airbeaten");        Destroy (gameobject,1.5f); 3 seconds after Game object destruction    //}    break;

  }               }     private void Fbackpoint ()  {        transform. LookAt (New Vector3 (Srcpoint.x, TRANSFORM.POSITION.Y, Srcpoint.y));       transform. Translate (vector3.forward*vescapespeed*time.deltatime);   }    public float backuptime;   //Backup time  private void Fattackhero ()  {          / /debug.log ("I attacked you!!!" ");     if (Time.time-backuptime > 1) {      player. SendMessage ("Beattack");    backUptime=Time.time;   }     }    private void Fpursuithero ()    {         transform. LookAt (player.transform.position);      transform. Translate (Vector3.forward * runspeed * TimE.deltatime);     //transform. Translate (Vector3.forward * vescapespeed * time.deltatime);   }     private void Fpatrovrect ()     {        transform. LookAt (New Vector3 (DIS.X,TRANSFORM.POSITION.Y,DIS.Y));         transform. Translate (Vector3.forward * speed * time.deltatime);     }    private void Fgetpatrolposition ()    {         vector2 vcur;         do         {             float MinX = srcpoint.x-vrect.width/2;       Float miny = srcpoint.y-vrect.height/2;     float MaxX = srcpoint.x + vrect.width/2;      float Maxy = srcpoint.y + vrect.height/2;    &Nbsp;float DISX = Random.range (MinX, MaxX);          float DisY = Random.range (Miny, maxy);          dis = new Vector2 (DisX, DisY);             vcur = new Vector2 (transform.position.x, TRANSFORM.POSITION.Z);    }        while (Vector2.distance (Dis, Vcur) < 5);    }  public float hp=3;  //accepts the killing from the protagonist  public void  beattackmoster ()  {  debug.log ("I was killed by you!!! "); Accept the   if from the protagonist Hp>0   {   HP--;    debug.log ( Gameobject.name+ "HP:" +HP);   }   else   {   debug.log (gameobject+ "death");     Vstartstate=dead;   }

} }

Unity Monster AI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.