Unity之机器人AI

接着上篇简单机器人AI的那个,下午搜了下相关的看到“Unity3D研究院之游戏开发中的人工智能AI(三十八)”,便仔细尝试按照他的思路着写了并测试了,效果杠杠的

我就贴自已写的一遍的代码了,作为一个小小小笔记吧!!!

  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 /// <summary>
  5 /// 类型
  6 /// </summary>
  7 public enum RobotAIType
  8 {
  9     Gremlins,
 10     Monster,
 11     Boss,
 12 }
 13 
 14 /// <summary>
 15 /// 状态
 16 /// </summary>
 17 public enum Robot_Do_type
 18 {
 19     Robot_idle=0,
 20     Robot_run=1,
 21     Robot_pause=2,
 22     Robot_walk=3,
 23     Robot_chasePlayer=4,
 24     Robot_attackPlayer=5
 25 }
 26 
 27 public class Robot : MonoBehaviour
 28 {
 29     //怪物
 30     public RobotAIType robotAIType = RobotAIType.Gremlins;
 31     //主角
 32     public GameObject player;
 33     //状态
 34     private Robot_Do_type robotState;
 35     private float robotStopThinkTime;
 36     public float robotThinkingTime = 5f;
 37 
 38     Robot_Do_type currentState;
 39 
 40     public void Start()
 41     {
 42         robotState = Robot_Do_type.Robot_idle;
 43         StartCoroutine(RobotAI());
 44     }
 45 
 46     IEnumerator RobotAI()
 47     {
 48         while (enabled)
 49         {
 50             switch (robotAIType)
 51             {
 52                 case RobotAIType.Boss:
 53                     BossExecute();
 54                     break;
 55                 case RobotAIType.Gremlins:
 56                     GremlinsExecute();
 57                     break;
 58                 case RobotAIType.Monster:
 59                     MonsterExecute();
 60                     break;
 61             }
 62             yield return null;
 63         }
 64         yield return null;
 65     }
 66 
 67     private void GremlinsExecute()
 68     {
 69         //判断精灵与主角的距离
 70         if (Vector3.Distance(player.transform.position, transform.position) <= 10f)
 71         {
 72             transform.LookAt(player.transform);
 73         }
 74     }
 75 
 76     private void MonsterExecute()
 77     {
 78         if (IsThinking())
 79         {
 80             AIThinking();
 81         }
 82         else
 83         {
 84             UpdateState();
 85         }
 86     }
 87 
 88     public void BossExecute()
 89     {
 90 
 91     }
 92 
 93     bool IsThinking()
 94     {
 95         if (Time.time - robotStopThinkTime >= 3f)
 96         {
 97             robotStopThinkTime = Time.time;
 98             return true;
 99         }
100         return false;
101     }
102 
103     void AIThinking()
104     {
105         int r = Random.Range(0, 4);
106         switch (r)
107         {
108             case (int)Robot_Do_type.Robot_idle:
109                 SetRobotState(Robot_Do_type.Robot_idle);
110                 break;
111             case (int)Robot_Do_type.Robot_pause:
112                 SetRobotState(Robot_Do_type.Robot_pause);
113                 break;
114             case (int)Robot_Do_type.Robot_run:
115                 SetRobotState(Robot_Do_type.Robot_run);
116                 break;
117             case (int)Robot_Do_type.Robot_walk:
118                 SetRobotState(Robot_Do_type.Robot_walk);
119                 break;
120         }
121     }
122 
123     void SetRobotState(Robot_Do_type type)
124     {
125         if (currentState == type)
126             return;
127         currentState = type;
128         switch (type)
129         {
130             case Robot_Do_type.Robot_attackPlayer:
131                 transform.LookAt(player.transform);
132                 Debug.Log("attackPlayer attackPlayer");
133                 break;
134             case Robot_Do_type.Robot_chasePlayer:
135                 transform.LookAt(player.transform);
136                 Debug.Log("chasePlayer chasePlayer");
137                 break;
138             case Robot_Do_type.Robot_idle:
139                 Debug.Log("idle idle idle idle");
140                 break;
141             case Robot_Do_type.Robot_pause:
142                 Debug.Log("pause pause pause pause");
143                 break;
144             case Robot_Do_type.Robot_run:
145                 Debug.Log("run run run run");
146                 break;
147             case Robot_Do_type.Robot_walk:
148                 Debug.Log("walk walk walk walk");
149                 break;
150         }
151         //如果是播放动画,判断动画是否正在播放,若没播放则播放
152     }
153 
154 
155     private void UpdateState()
156     {
157        //判断与主角的距离
158         float distance = Vector3.Distance(player.transform.position, transform.position);
159         if(distance>=10f)
160         {
161             SetRobotState(Robot_Do_type.Robot_idle);
162         }
163         else
164         {
165             if(distance<3f)
166             {
167                 SetRobotState(Robot_Do_type.Robot_attackPlayer);
168             }
169             else
170             {
171                 SetRobotState(Robot_Do_type.Robot_chasePlayer);
172             }
173         }
174     }
175 }

下面是我测试的小图图:

一张0.4M的图为什么上传不上去咧!!!不高兴,不传了

---------------------------------------------------------------------

我的小啊小啊小笔记

原文地址:https://www.cnblogs.com/cathytong/p/4673999.html