[Unity] Unity人工智能学习—确定性AI算法之追踪算法一

查看:2273 |回复:49 | 2015-8-17 09:45:48

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

x
本帖最后由 成林 于 2018-5-16 21:29 编辑

作者原话:
16.jpg
尽管随机运动可能完全不可预知,它还是相当无趣的,因为它完全是以相同的方式工作——完全随机。


下面要学习到的算法是根据具体环境作出不同响应的处理。


作为例子,这里选择了追踪算法。追踪AI考虑到跟踪目标的位置,然后改变AI对象的轨道好让它移向被追踪的对象。


追踪可以是将方向矢量直接指向目标,或者采用跟真实的模型,使得物体像导弹那样行动。


20150812212951357.png

本文将的就是第一种,直接矢量追踪,效果如下图所示:





通过键盘方向键控制幽灵,然后蚊子会追踪幽灵,
在这里我做了一些界面处理,
当蚊子或者幽灵运动超过了屏幕范围的时候让它们出现在屏幕的另一边而不是消失在屏幕上
代码如下:



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

  4. public class AITrack : MonoBehaviour {
  5.     public Image target;
  6.     public float moveSpeed;//追踪目标移动速度
  7.     public float targetSpeed;//追踪速度
  8.     public float target_x;//追踪移动的单位量
  9.     public float target_y;
  10.         // Use this for initialization
  11.         void Start () {
  12.             
  13.         }
  14.         // Update is called once per frame
  15.         void Update () {
  16.         MoveTarget();
  17.         Track_AI();
  18.         }
  19.     void Track_AI()
  20.     {
  21.         //x方向的追踪
  22.         if(target.transform.position.x>this.transform.position.x)
  23.         {
  24.             this.transform.position += new Vector3(target_x, 0, 0)*targetSpeed;
  25.         }
  26.         else if(target.transform.position.x<this.transform.position.x)
  27.         {
  28.             this.transform.position -= new Vector3(target_x, 0, 0)*targetSpeed;
  29.         }
  30.         //y方向的追踪
  31.         if(target.transform.position.y>this.transform.position.y)
  32.         {
  33.             this.transform.position += new Vector3(0, target_y, 0)*targetSpeed;
  34.         }
  35.         else if(target.transform.position.y<this.transform.position.y)
  36.         {
  37.             this.transform.position -= new Vector3(0, target_y, 0)*targetSpeed;
  38.         }
  39.         //检测是否超出了边界
  40.         if(this.transform.position.x>=Screen.width)
  41.         {
  42.             this.transform.position = new Vector3(-this.GetComponent<Image>().rectTransform.lossyScale.x, 0, 0);
  43.         }
  44.         else if(this.transform.position.x<-this.GetComponent<Image>().rectTransform.lossyScale.x)
  45.         {
  46.             this.transform.position = new Vector3(Screen.width, this.transform.position.y, 0);
  47.         }
  48.         if(this.transform.position.y>=Screen.height)
  49.         {
  50.             this.transform.position = new Vector3(this.transform.position.x,-this.GetComponent<Image>().rectTransform.lossyScale.y, 0);
  51.         }
  52.         else if(this.transform.position.y<-this.GetComponent<Image>().rectTransform.lossyScale.y)
  53.         {
  54.             this.transform.position = new Vector3(this.transform.position.x, Screen.height, 0);
  55.         }
  56.     }
  57.     void MoveTarget()
  58.     {
  59.         float x = Input.GetAxis("Horizontal")*100;
  60.         float y = Input.GetAxis("Vertical")*100;

  61.         target.transform.Translate(x*Time.deltaTime*moveSpeed,y*Time.deltaTime*moveSpeed,0);
  62.         //如果超出屏幕范围则让它出现在另一面
  63.         if (target.transform.position.x >= Screen.width)
  64.         {
  65.             //使用了Image的target.rectTransform.lossyScale.x来表示显示的图片宽度
  66.             target.transform.position = new Vector3(-target.rectTransform.lossyScale.x, target.transform.position.y, 0);
  67.         }
  68.         else if(target.transform.position.x<-target.rectTransform.lossyScale.x)
  69.         {
  70.             target.transform.position = new Vector3(Screen.width, target.transform.position.y, 0);
  71.         }
  72.         if(target.transform.position.y>=Screen.height)
  73.         {
  74.             target.transform.position = new Vector3(target.transform.position.x, -target.rectTransform.lossyScale.y, 0);
  75.         }
  76.         else if(target.transform.position.y<-target.rectTransform.lossyScale.y)
  77.         {
  78.             target.transform.position = new Vector3(target.transform.position.x, Screen.height, 0);
  79.         }
  80.     }
  81. }
点击此处复制文本


原帖地址:http://blog.csdn.net/zhangxiao13 ... le/details/47451063

评分

参与人数 2元素币 +10 活跃度 +13 展开 理由
神月辉夜 + 10 阅贴无数,楼主最强!
狼之独步 + 10 + 3 阅贴无数,楼主最强!

查看全部评分

2015-8-17 09:45:48  
 赞 赞 1

使用道具 登录

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

使用道具 登录

3#
阅贴无数,楼主最强!
回复 收起回复
2015-8-17 11:04:35   回复
 赞 赞 1

使用道具 登录

4#
带你赚币带你飞,元素里面有正妹!
回复 收起回复
2015-8-18 09:43:25   回复
 赞 赞 1

使用道具 登录

5#
给力!元素有你更精彩
回复 收起回复
2015-8-18 15:13:17   回复
 赞 赞 1

使用道具 登录

6#
感谢分享教程
回复 收起回复
2015-8-18 20:19:27   回复
 赞 赞 1

使用道具 登录

7#
为了元素币,拼了!
回复 收起回复
2015-8-18 21:48:39   回复
 赞 赞 1

使用道具 登录

8#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-9-16 10:04:55   回复
 赞 赞 1

使用道具 登录

9#
想要成大触,天天上元素!
回复 收起回复
2015-9-16 10:05:09   回复
 赞 赞 1

使用道具 登录

10#
给力!元素有你更精彩
回复 收起回复
2015-9-16 10:05:22   回复
 赞 赞 1

使用道具 登录

11#
埃索镁拉唑
回复 收起回复
2015-9-16 15:18:04   回复
 赞 赞 1

使用道具 登录

12#
想要成大触,天天上元素!
回复 收起回复
2015-9-17 07:51:32   回复
 赞 赞 1

使用道具 登录

13#
http://www.element3ds.com/forum.php?mod=viewthread&tid=1185&extra=page=1
回复 收起回复
2015-9-26 09:04:21   回复
 赞 赞 1

使用道具 登录

14#
{:1_145:}
回复 收起回复
2015-9-26 15:10:55   回复
 赞 赞 1

使用道具 登录

15#
带你赚币带你飞,元素里面有正妹!
回复 收起回复
2015-9-29 14:30:31   回复
 赞 赞 1

使用道具 登录

16#
给力!元素有你更精彩
回复 收起回复
2015-10-3 11:21:11   回复
 赞 赞 1

使用道具 登录

17#
为了元素币,拼了!
回复 收起回复
2015-10-5 13:10:52   回复
 赞 赞 1

使用道具 登录

18#
不错很好很强大
回复 收起回复
2015-10-11 13:03:27   回复
 赞 赞 1

使用道具 登录

19#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-10-19 21:24:20   回复
 赞 赞 1

使用道具 登录

20#
天下武功出少林,世界资源入元素!
回复 收起回复
2016-1-25 11:07:55   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

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

本版积分规则

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