转自http://blog.csdn.net/zhangxiao13627093203/article/details/47451063

尽管随机运动可能完全不可预知,它还是相当无趣的,因为它完全是以相同的方式工作——完全随机。下面要学习到的算法是根据具体环境作出不同响应的处理。作为例子,这里选择了追踪算法。追踪AI考虑到跟踪目标的位置,然后改变AI对象的轨道好让它移向被追踪的对象。

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

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

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

代码如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class AITrack : MonoBehaviour {
public Image target;
public float moveSpeed;//追踪目标移动速度
public float targetSpeed;//追踪速度
public float target_x;//追踪移动的单位量
public float target_y;
// Use this for initialization
void Start () { }
// Update is called once per frame
void Update () {
MoveTarget();
Track_AI();
}
void Track_AI()
{
//x方向的追踪
if(target.transform.position.x>this.transform.position.x)
{
this.transform.position += new Vector3(target_x, 0, 0)*targetSpeed;
}
else if(target.transform.position.x<this.transform.position.x)
{
this.transform.position -= new Vector3(target_x, 0, 0)*targetSpeed;
}
//y方向的追踪
if(target.transform.position.y>this.transform.position.y)
{
this.transform.position += new Vector3(0, target_y, 0)*targetSpeed;
}
else if(target.transform.position.y<this.transform.position.y)
{
this.transform.position -= new Vector3(0, target_y, 0)*targetSpeed;
}
//检测是否超出了边界
if(this.transform.position.x>=Screen.width)
{
this.transform.position = new Vector3(-this.GetComponent<Image>().rectTransform.lossyScale.x, 0, 0);
}
else if(this.transform.position.x<-this.GetComponent<Image>().rectTransform.lossyScale.x)
{
this.transform.position = new Vector3(Screen.width, this.transform.position.y, 0);
}
if(this.transform.position.y>=Screen.height)
{
this.transform.position = new Vector3(this.transform.position.x,-this.GetComponent<Image>().rectTransform.lossyScale.y, 0);
}
else if(this.transform.position.y<-this.GetComponent<Image>().rectTransform.lossyScale.y)
{
this.transform.position = new Vector3(this.transform.position.x, Screen.height, 0);
}
}
void MoveTarget()
{
float x = Input.GetAxis("Horizontal")*100;
float y = Input.GetAxis("Vertical")*100; target.transform.Translate(x*Time.deltaTime*moveSpeed,y*Time.deltaTime*moveSpeed,0);
//如果超出屏幕范围则让它出现在另一面
if (target.transform.position.x >= Screen.width)
{
//使用了Image的target.rectTransform.lossyScale.x来表示显示的图片宽度
target.transform.position = new Vector3(-target.rectTransform.lossyScale.x, target.transform.position.y, 0);
}
else if(target.transform.position.x<-target.rectTransform.lossyScale.x)
{
target.transform.position = new Vector3(Screen.width, target.transform.position.y, 0);
}
if(target.transform.position.y>=Screen.height)
{
target.transform.position = new Vector3(target.transform.position.x, -target.rectTransform.lossyScale.y, 0);
}
else if(target.transform.position.y<-target.rectTransform.lossyScale.y)
{
target.transform.position = new Vector3(target.transform.position.x, Screen.height, 0);
}
}
}

  

Unity人工智能学习—确定性AI算法之追踪算法一的更多相关文章

  1. Unity人工智能学习—确定性AI算法之追踪算法二

    转自:http://blog.csdn.net/zhangxiao13627093203/article/details/47658673 上一篇讲到了追踪算法的比较简单的形式,看上去比较假,因为AI ...

  2. AI 经典书单 | 人工智能学习该读哪些书

    转载 2018年01月16日 00:00:00   人工智能相关岗位中,涉及到的内容包含: 算法.深度学习.机器学习.自然语言处理.数据结构.Tensorflow.Python .数据挖掘.搜索开发. ...

  3. 游戏人工智能 读书笔记 (四) AI算法简介——Ad-Hoc 行为编程

    本文内容包含以下章节: Chapter 2 AI Methods Chapter 2.1 General Notes 本书英文版: Artificial Intelligence and Games ...

  4. 【中国象棋人机对战】引入了AI算法,学习低代码和高代码如何混编并互相调用

    以低代码和高代码(原生JS代码)混编的方式引入了AI算法,学习如何使用表达式调用原生代码的.整个过程在众触低代码应用平台进行,适合高阶学员. AI智能级别演示 AI算法分三个等级,体现出来的智能水平不 ...

  5. 【AI】微软人工智能学习笔记(一)

    数据分析平台 01|数据平台概况图示 上面图中所示就是微软人工智能数据平台的相关的技术. 02.1| Cortana Intelligence Suite 从上面图中可以看到, 其中有一个Cortan ...

  6. AI人工智能学习数据集

    AI人工智能学习数据集,列表如下. 商务合作,科技咨询,版权转让:向日葵,135—4855__4328,xiexiaokui#qq.com boston_house_prices.csvbreast_ ...

  7. 聊聊找AI算法岗工作

    https://blog.csdn.net/weixin_42137700/article/details/81628028 首先,本文不是为了增加大家的焦虑感,而是站在一名学生的角度聊聊找AI算法岗 ...

  8. 2017人工智能元年,AI在喧嚣和质疑中一路走来

    前百度首席科学家吴恩达说:就像100年前的电力.20年前的互联网一样,AI也会改变每一个产业! 有人说,现在就像1995年,那一年,第一家互联网公司--网景上市,一天之内大涨208%,互联网正式登上历 ...

  9. 曼孚科技:AI算法领域常用的39个术语(下)

    算法是人工智能(AI)核心领域之一. 本文整理了算法领域常用的39个术语,希望可以帮助大家更好地理解这门学科. 本文为下半部分,上半部分见本账号上一篇文章. 19.迁移学习(Transfer Lear ...

随机推荐

  1. vs2015开发Windows服务

    工作已经很久,时隔这么长时间写这篇文章是给自己以后做参考.也不至于以后长时间不写Windows服务而忘记整个开发过程.windows服务开发,基础的就不说了,直接上过程. 1.新建windows服务项 ...

  2. VS2003与Win7的兼容性问题

    VS2003并不官方支持Win7,目前大家遇到的主要是两个问题: 1. 在debug的时候,*.pdb文件在会被devenv.exe给lock住,当调适过一次之后,再进行编译的时候会有lnk1201的 ...

  3. Android开发效率的小技巧

    提高eclipse使用效率(二) 提高Android开发效率的小技巧   XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Wi ...

  4. Linux环境fork()函数详解

    Linux环境fork()函数详解 引言 先来看一段代码吧, 1 #include <sys/types.h> 2 #include <unistd.h> 3 #include ...

  5. JavaScript中变量声明有var和没var的区别

    JavaScript中变量声明有var和没var的区别 JavaScript中有var和没var的区别 Js中的变量声明的作用域是以函数为单位,所以我们经常见到避免全局变量污染的方法是 (functi ...

  6. 【C++自我精讲】基础系列四 static

    [C++自我精讲]基础系列四 static 0 前言 变量的存储类型:存储类型按变量的生存期划分,分动态存储方式和静态存储方式. 1)动态存储方式的变量,生存期为变量所在的作用域.即程序运行到此变量时 ...

  7. Acoustic Echo Cancellation (AEC) 回音消除技术探索

            回声产生的原因: 本地产生的音频信息通过网络传输到远端, 远端音频信号通过反射再由远端麦克采集到远端系统,再通过IP网络传输本地,本地播放后,在由本地麦克采集到,这就构成了类似闭环正反 ...

  8. kubernetes service分析

    service type k8s中service主要有三种: ClusterIP: use a cluster-internal IP only - this is the default and i ...

  9. $.each()方法,其实挺不错的

    例子为主 html主要代码 <div class="fl search">厂商:<select id="firms"><optio ...

  10. IdentityServer4 实现 OpenID Connect 和 OAuth 2.0

    关于 OAuth 2.0 的相关内容,点击查看:ASP.NET WebApi OWIN 实现 OAuth 2.0 OpenID 是一个去中心化的网上身份认证系统.对于支持 OpenID 的网站,用户不 ...