【转】 Unity3D 野怪AI(巡逻,追击,攻击)以及Hero的移动及旋转
原文地址:http://bbs.9ria.com/thread-179173-1-1.html
野怪AI
1 [hide]using UnityEngine; 2 using System.Collections; 3 4 public class NewMonsterAI : MonoBehaviour { 5 6 public const int AI_Null = 10; 7 public const int AI_Idle = 0; 8 public const int AI_Patrol =1; 9 public const int AI_Pursuit =2; 10 public const int AI_Attack = 3; 11 public const int AI_Attack_Idle = 4; 12 public const int AIA_BackPoint = 5; 13 14 public const float followUpDistance = 3.0f; 15 public const float attackDistance = 2.0f; 16 17 public const float vAttackDeltaTime = 1.0f; 18 19 public const float maxFollow = 20.0f; 20 public const float speed = 2.0f; 21 public const float runSpeed = 3.0f; 22 public const float vEscapeSpeed = 6.0f; 23 24 public Rect vRect = new Rect(0,0,10,10); 25 26 public Vector2 Dis; 27 public Vector2 srcPoint;//出生坐标 28 29 public GameObject player; 30 public int vStartState; 31 public int vLostState; 32 // Use this for initialization 33 void Start () { 34 vStartState = AI_Idle; 35 vLostState = AI_Null; 36 srcPoint = new Vector2(transform.position.x,transform.position.z); 37 player = GameObject.Find("Viking"); 38 } 39 40 // Update is called once per frame 41 void Update () { 42 switch (vStartState) 43 { 44 case AI_Idle: 45 if (vStartState != vLostState) 46 { 47 animation.CrossFade("Idle"); 48 vLostState = vStartState; 49 Debug.Log("1"); 50 } 51 fGetPatrolPosition(); 52 vStartState = AI_Patrol; 53 break; 54 case AI_Patrol: 55 if (vStartState != vLostState) 56 { 57 animation.CrossFade("Walk"); 58 vLostState = vStartState; 59 Debug.Log("2"); 60 } 61 fPatrovRect(); 62 if(Vector2.Distance(Dis,new Vector2(transform.position.x,transform.position.z))<0.4f) 63 { 64 //Debug.Log("没进来?"); 65 66 vStartState = AI_Idle; 67 } 68 if (Vector3.Distance(transform.position, player.transform.position) < followUpDistance) 69 { 70 vStartState = AI_Pursuit; 71 } 72 73 break; 74 case AI_Pursuit: 75 if (vStartState != vLostState) 76 { 77 animation.CrossFade("Run"); 78 vLostState = vStartState; 79 Debug.Log("3"); 80 } 81 fPursuitHero(); 82 if (Vector3.Distance(new Vector3(srcPoint.x, transform.position.y, srcPoint.y), transform.position) > maxFollow && Vector3.Distance(transform.position, player.transform.position) > followUpDistance) 83 { 84 vStartState = AIA_BackPoint; 85 } 86 if (Vector3.Distance(transform.position, player.transform.position) < attackDistance) 87 { 88 vStartState = AI_Attack; 89 } 90 break; 91 case AI_Attack: 92 if (vStartState != vLostState) 93 { 94 vLostState = vStartState; 95 Debug.Log("4"); 96 } 97 fAttackHero(); 98 if (Time.time - backUptime >= 0.2f) 99 { 100 backUptime = Time.time; 101 vStartState = AI_Attack_Idle; 102 } 103 break; 104 case AI_Attack_Idle: 105 if (vStartState != vLostState) 106 { 107 animation.CrossFade("Idle"); 108 vLostState = vStartState; 109 } 110 vStartState = AI_Pursuit; 111 break; 112 case AIA_BackPoint: 113 if (vStartState != vLostState) 114 { 115 animation.CrossFade("Run"); 116 vLostState = vStartState; 117 } 118 fBackPoint(); 119 if (Vector2.Distance(srcPoint,new Vector2( transform.position.x, transform.position.z) )< 1) 120 { 121 vStartState = AI_Idle; 122 } 123 break; 124 } 125 126 } 127 128 private void fBackPoint() 129 { 130 transform.LookAt(new Vector3(srcPoint.x, transform.position.y, srcPoint.y)); 131 transform.Translate(Vector3.forward*vEscapeSpeed*Time.deltaTime); 132 } 133 public float backUptime; 134 private void fAttackHero() 135 { 136 //Debug.Log("我攻击到你了!!!"); 137 } 138 139 private void fPursuitHero() 140 { 141 transform.LookAt(player.transform.position); 142 transform.Translate(Vector3.forward * runSpeed * Time.deltaTime); 143 //transform.Translate(Vector3.forward * vEscapeSpeed * Time.deltaTime); 144 } 145 146 private void fPatrovRect() 147 { 148 transform.LookAt(new Vector3(Dis.x,transform.position.y,Dis.y)); 149 transform.Translate(Vector3.forward * speed * Time.deltaTime); 150 } 151 152 private void fGetPatrolPosition() 153 { 154 Vector2 vCur; 155 do 156 { 157 float minX = srcPoint.x - vRect.width / 2; 158 float minY = srcPoint.y - vRect.height / 2; 159 float maxX = srcPoint.x + vRect.width / 2; 160 float maxY = srcPoint.y + vRect.height / 2; 161 float disX = Random.Range(minX, maxX); 162 float disY = Random.Range(minY, maxY); 163 Dis = new Vector2(disX, disY); 164 vCur = new Vector2(transform.position.x,transform.position.z); 165 } 166 while (Vector2.Distance(Dis,vCur) < 5); 167 } 168 }[/hide]