Unity之简单的机器人AI状态改变

最近在玩天涯明月刀,还蛮好玩,以前都不爱玩游戏,当然啦我现在升到38级了搁着也没玩了,不过曲无忆看外形真没有御姐的范倒像个萌妹子,可是慕情偏是个御姐的外形萌妹子的心啊啊啊

这两天看到知乎有有关于面试的问题,然后看到有关智能AI的,就想到天涯明月刀里做任务时的打怪,怪老是一会走啊走一会干嘛干嘛的,做VR的我还真没接触过咧

那就来看看呗,反正不掉肉不长肉的

这个是只是状态的改变,如果检测到周围有敌人或者啥的然后打啊打杀啊杀啊后面再加吧!

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Robot : MonoBehaviour
 5 {
 6     /// <summary>
 7     /// 状态
 8     /// </summary>
 9     public enum Robot_Do_type
10     {
11         None,
12         Robot_idle,
13         Robot_run,
14         Robot_pause,
15         Robot_walk
16     }
17 
18     //状态
19     private Robot_Do_type robotState;
20     private float robotStopThinkTime;
21     public float robotThinkingTime = 5f;
22 
23     public void Start()
24     {
25         robotState = Robot_Do_type.Robot_idle;
26         StartCoroutine(RobotAI());
27     }
28 
29     IEnumerator RobotAI()
30     {
31         while (enabled)
32         {
33             if (Time.time - robotStopThinkTime >= robotThinkingTime)
34             {
35                 robotStopThinkTime = Time.time;
36                 int ran = Random.Range(0, 4);//随机分配状态
37                 if (ran == 0)
38                 {
39                     robotState = Robot_Do_type.Robot_idle;
40                 }
41                 else if (ran == 1)
42                 {
43                     robotState = Robot_Do_type.Robot_walk;
44                 }
45                 else if (ran == 2)
46                 {
47                     robotState = Robot_Do_type.Robot_pause;
48                 }
49                 else if (ran == 3)
50                 {
51                     robotState = Robot_Do_type.Robot_run;
52                     Quaternion q = Quaternion.Euler(0, Random.Range(1, 5) * 90, 0);
53                     transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 100f);
54                 }
55                 else
56                 {
57                     robotState = Robot_Do_type.None;
58                 }
59                 Execute();
60             }
61             yield return null;
62         }
63         yield return null;
64     }
65 
66     /// <summary>
67     /// 根据机器状态来执行相对应的操作
68     /// </summary>
69     public void Execute()
70     {
71         switch (robotState)
72         {
73             case Robot_Do_type.Robot_idle:
74                 Debug.Log("idle idle idle idle idle idle idle idle idle idle idle");
75                 break;
76             case Robot_Do_type.Robot_pause:
77                 Debug.Log("pause pause pause pause pause pause pause pause pause pause");
78                 break;
79             case Robot_Do_type.Robot_run:
80                 Debug.Log("run run run run run run run run run run run run run run run");
81                 break;
82             case Robot_Do_type.Robot_walk:
83                 Debug.Log("walk walk walk walk walk walk walk walk walk walk walk walk");
84                 break;
85             case Robot_Do_type.None:
86                 Debug.Log("None None None None None None None None None None None None");
87                 break;
88         }
89     }
90 }

 

这个就是打印出来的样子,那如果把Debug.Log给替换下成你所需要的操作就O了

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

个人笔记--

posted @ 2015-07-24 14:08  TTT童童童  阅读(1310)  评论(0编辑  收藏  举报