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

查看:968 |回复:14 | 2015-11-9 17:49:03

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

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

关于AI的介绍我们在前面提到过,本篇文章所写内容都是平时学习人工智能的时候有意的将它们转换成Unity 的实现版本。所谓确定性算法是一些预先确定或者预先编程的操作,比如《星球大战》游戏中的小行星的AI都是非常简单的,都是以随机速率将其沿着随机的方向发射出去,这是一种最简单的智能,不过它们的智能都是相当确定和可预知的。
而确定性AI算法里面,随机运动又是最简单的,如图,我设置了移动随机和停留随机时间,这样看上去才更加真实。
Center.jpg 贴上代码
80using UnityEngine;
using System.Collections;
public class AIRandMove : MonoBehaviour
{
float stopTime;
float moveTime;
float vel_x, vel_y, vel_z;//速度
///
/// 最大、最小飞行界限
///
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);
}
}
}
好了本篇unity3d教程关于人工智能确定性AI算法之随机运动到此结束,下篇我们再会! image.png



2015-11-9 17:49:03  
 赞 赞 1

使用道具 登录

14个回答,把该问题分享到群,邀请大神一起回答。
2#
MARK 已经收藏
回复 收起回复
2015-11-10 07:05:58   回复
 赞 赞 1

使用道具 登录

3#
元素那么大,我想来看看!
回复 收起回复
2015-11-10 07:35:20   回复
 赞 赞 1

使用道具 登录

4#
带你赚币带你飞,元素里面有正妹!
回复 收起回复
2015-11-10 07:35:35   回复
 赞 赞 1

使用道具 登录

6#
好贴          {:1_152:}顶
回复 收起回复
2015-11-10 10:20:55   回复
 赞 赞 1

使用道具 登录

7#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-11-10 18:29:44   回复
 赞 赞 1

使用道具 登录

8#
想要成大触,天天上元素!
回复 收起回复
2015-11-12 09:12:29   回复
 赞 赞 1

使用道具 登录

9#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-11-12 12:00:04   回复
 赞 赞 1

使用道具 登录

10#

MARK 已经收藏
回复 收起回复
2015-11-12 13:36:06   回复
 赞 赞 1

使用道具 登录

11#
元素帖子强,满满正能量!
回复 收起回复
2015-11-13 08:56:20   回复
 赞 赞 1

使用道具 登录

12#
使用随机数进行控制?
回复 收起回复
2015-11-18 15:54:40   回复
 赞 赞 1

使用道具 登录

13#
感谢分享{:1_144:}
回复 收起回复
2015-11-24 23:46:10   回复
 赞 赞 1

使用道具 登录

14#
不明觉厉... 马克一下...
回复 收起回复
2015-11-25 19:52:12   回复
 赞 赞 1

使用道具 登录

15#
收藏走一波。
回复 收起回复
2018-6-2 21:50:05   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

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

本版积分规则

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