Unity人工智能学习—确定性AI算法之随机运动
2015-08-12 16:00
603 查看
以下所写内容都是平时学习人工智能的时候有意的将它们转换成Unity的实现版本。所谓确定性算法是一些预先确定或者预先编程的操作,比如《星球大战》游戏中的小行星的AI都是非常简单的,都是以随机速率将其沿着随机的方向发射出去,这是一种最简单的智能,不过它们的智能都是相当确定和可预知的。
而确定性AI算法里面,随机运动又是最简单的,如图
我设置了移动随机和停留随机时间,这样看上去才更加真实。
而确定性AI算法里面,随机运动又是最简单的,如图
我设置了移动随机和停留随机时间,这样看上去才更加真实。
using UnityEngine; using System.Collections; public class AIRandMove : MonoBehaviour { float stopTime; float moveTime; float vel_x, vel_y, vel_z;//速度 /// <summary> /// 最大、最小飞行界限 /// </summary> float maxPos_x = 500; float maxPos_y = 300; float minPos_x = -500; float minPos_y = -300; int curr_frame; int total_frame; float timeCounter1; float timeCounter2; // int max_Flys = 128; // Use this for initialization void Start() { Change(); } // Update is called once per frame void Update() { timeCounter1 += Time.deltaTime; if (timeCounter1 < moveTime) { transform.Translate(vel_x, vel_y, 0, Space.Self); } else { timeCounter2 += Time.deltaTime; if (timeCounter2 > stopTime) { Change(); timeCounter1 = 0; timeCounter2 = 0; } } Check(); } void Change() { stopTime = Random.Range(1, 5); moveTime = Random.Range(1, 20); vel_x = Random.Range(1, 10); vel_y = Random.Range(1, 10); } void Check() { //如果到达预设的界限位置值,调换速度方向并让它当前的坐标位置等于这个临界边的位置值 if (transform.localPosition.x > maxPos_x) { vel_x = -vel_x; transform.localPosition = new Vector3(maxPos_x, transform.localPosition.y, 0); } if (transform.localPosition.x < minPos_x) { vel_x = -vel_x; transform.localPosition = new Vector3(minPos_x, transform.localPosition.y, 0); } if (transform.localPosition.y > maxPos_y) { vel_y = -vel_y; transform.localPosition = new Vector3(transform.localPosition.x, maxPos_y, 0); } if (transform.localPosition.y < minPos_y) { vel_y = -vel_y; transform.localPosition = new Vector3(transform.localPosition.x, minPos_y, 0); } } }
相关文章推荐
- unity, use particleSystem with UGUI
- Unity 时间的具体使用
- Unity3D-重新编译Mono加密DLL
- unity初写游戏(四)怪物产生方式
- unity初写游戏(三)人物的瞬移
- unity初写游戏(二)照相机的写法
- Unity NGUI UIKeyBinding
- Win8下安装Unity5.1.2f1出现unity editor 已停止工作解决方法
- Unity debug mode下的watermark去除
- 【Unity3D】【NGUI】3.0+版本,粒子在UI后面显示(RenderQueue 渲染队列先后顺序)
- Unity笔记 Graphics( 光 shader camera mesh )
- unity初写游戏(一)2D界面GUIText
- Unity shader学习资料
- 使用Unity3D的50个技巧:Unity3D最佳实践(上)
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(49)-工作流设计-我的申请
- Unity3D_WebPlayer_PHP连接MySQL数据库_3_php登录和读取所有数据
- Unity3D_WebPlayer_PHP连接MySQL数据库_2_php注册逻辑
- Unity3D_WebPlayer_PHP连接MySQL数据库_1_php服务器配置
- Unity3D--EventBase
- Unity3D 连接MySQL数据库笔记3-SQL建表