[Unity] Unity人工智能学习—确定性AI算法之随机运动

查看:2471 |回复:10 | 2015-8-17 09:38:59

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 大西几 于 2021-2-24 16:11 编辑


作者原话:
以下所写内容都是平时学习人工智能的时候有意的将它们转换成Unity的实现版本。


所谓确定性算法是一些预先确定或者预先编程的操作,


比如《星球大战》游戏中的小行星的AI都是非常简单的,都是以随机速率将其沿着随机的方向发射出去,


这是一种最简单的智能,不过它们的智能都是相当确定和可预知的。


而确定性AI算法里面,随机运动又是最简单的,如图



20150812154227875.gif

我设置了移动随机和停留随机时间,这样看上去才更加真实。

  1. using UnityEngine;
  2. using System.Collections;

  3. public class AIRandMove : MonoBehaviour
  4. {
  5.     float stopTime;
  6.     float moveTime;
  7.     float vel_x, vel_y, vel_z;//速度
  8.     /// <summary>
  9.     /// 最大、最小飞行界限
  10.     /// </summary>
  11.     float maxPos_x = 500;
  12.     float maxPos_y = 300;
  13.     float minPos_x = -500;
  14.     float minPos_y = -300;
  15.     int curr_frame;
  16.     int total_frame;
  17.     float timeCounter1;
  18.     float timeCounter2;
  19.     // int max_Flys = 128;
  20.     // Use this for initialization
  21.     void Start()
  22.     {
  23.         Change();

  24.     }

  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.         timeCounter1 += Time.deltaTime;
  29.         if (timeCounter1 < moveTime)
  30.         {
  31.             transform.Translate(vel_x, vel_y, 0, Space.Self);
  32.         }
  33.         else
  34.         {
  35.             timeCounter2 += Time.deltaTime;
  36.             if (timeCounter2 > stopTime)
  37.             {
  38.                 Change();
  39.                 timeCounter1 = 0;
  40.                 timeCounter2 = 0;
  41.             }
  42.         }
  43.         Check();
  44.     }
  45.     void Change()
  46.     {
  47.         stopTime = Random.Range(1, 5);
  48.         moveTime = Random.Range(1, 20);
  49.         vel_x = Random.Range(1, 10);
  50.         vel_y = Random.Range(1, 10);
  51.     }
  52.     void Check()
  53.     {
  54.         //如果到达预设的界限位置值,调换速度方向并让它当前的坐标位置等于这个临界边的位置值
  55.         if (transform.localPosition.x > maxPos_x)
  56.         {
  57.             vel_x = -vel_x;
  58.             transform.localPosition = new Vector3(maxPos_x, transform.localPosition.y, 0);
  59.         }
  60.         if (transform.localPosition.x < minPos_x)
  61.         {
  62.             vel_x = -vel_x;
  63.             transform.localPosition = new Vector3(minPos_x, transform.localPosition.y, 0);
  64.         }
  65.         if (transform.localPosition.y > maxPos_y)
  66.         {
  67.             vel_y = -vel_y;
  68.             transform.localPosition = new Vector3(transform.localPosition.x, maxPos_y, 0);
  69.         }
  70.         if (transform.localPosition.y < minPos_y)
  71.         {
  72.             vel_y = -vel_y;
  73.             transform.localPosition = new Vector3(transform.localPosition.x, minPos_y, 0);
  74.         }
  75.     }

  76. }
点击此处复制文本


原帖地址:http://blog.csdn.net/zhangxiao13 ... le/details/47444623
image.png
2015-8-17 09:38:59  
 赞 赞 1

使用道具 登录

10个回答,把该问题分享到群,邀请大神一起回答。
2#
元素帖子强,满满正能量!
回复 收起回复
2015-8-17 09:54:53   回复
 赞 赞 1

使用道具 登录

3#
给力!元素有你更精彩
回复 收起回复
2015-8-17 10:09:58   回复
 赞 赞 1

使用道具 登录

4#
爱她就去优衣库,要耍就上微元素!
回复 收起回复
2015-8-17 13:28:00   回复
 赞 赞 1

使用道具 登录

5#
好贴!谢谢楼主分享学习经验,学习学习{:1_144:}
回复 收起回复
2015-8-18 02:20:52   回复
 赞 赞 1

使用道具 登录

6#
给力!元素有你更精彩
回复 收起回复
2015-8-26 19:25:41   回复
 赞 赞 1

使用道具 登录

7#
天下武功出少林,世界资源入元素!
回复 收起回复
2016-1-29 12:09:02   回复
 赞 赞 1

使用道具 登录

8#
很崇拜楼主,这个资源不错!
回复 收起回复
2016-2-14 00:52:56   回复
 赞 赞 1

使用道具 登录

9#
这个可以有
回复 收起回复
2016-2-20 08:42:15   回复
 赞 赞 1

使用道具 登录

10#
很崇拜楼主,这个资源不错!
回复 收起回复
2016-2-23 06:20:16   回复
 赞 赞 1

使用道具 登录

11#
找了很久终于找到你了!寡人死而无憾!
回复 收起回复
2016-2-24 02:26:37   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表