转自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. 获取时间SQL函数语句

    1.获取时间 获取当天的数据 where  DATEDIFF (DD, 数据库中时间的字段 ,GETDATE())=0 查询24小时内的 where  DATEDIFF (HH, 数据库中时间的字段 ...

  2. 正则表达式与领域特定语言(DSL)

    如何设计一门语言(十)——正则表达式与领域特定语言(DSL) 几个月前就一直有博友关心DSL的问题,于是我想一想,我在gac.codeplex.com里面也创建了一些DSL,于是今天就来说一说这个事情 ...

  3. 开启和禁用Wifi热点命令

    netsh wlan set hostednetwork mode=allow ssid=[无线网络名字] key=[密码] netsh wlan start hostednetwork --启用 禁 ...

  4. SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分

    SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分 辞职待业青年就是有很多时间来写博客,以前在传统行业技术强度相对不大,不处理大数据,也不弄高并发 ...

  5. MySQL中函数、游标、事件、视图基本应用举例(代码)

    MySQL中function用户自定义函数c,fun,fun是面向过程的实现方式只能传入参数,或不传入参数,不能传出参数,必有返回值函数中是不能有create table drop table之类的语 ...

  6. Chrome浏览器网页截全屏算法以及实现

    做个一个简单的批量下载插件叫“挖一下”, 正如插件的名字一样,采集网页里面的所有图片,根据筛选条件过滤不需要的图片,最后下载选中的图片. 索性把网页也一起给截了,截屏分两种: 1.可见内容截屏 2.完 ...

  7. 多线程计算----pthread

    #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h& ...

  8. window.setTimeout()函数的使用

    <script type="text/javascript"> //此程序主要完成页面定时关闭功能 function closeMyWindow() { window. ...

  9. 编程利用利用curses库编程开始

    时间紧张,先记一笔,后续优化与完善. curses库常用函数: 注意编译时要用这样的格式:gcc xxx.c -l curses -o xxx 第一个小例子: include <stdio.h& ...

  10. IFE-20 笔记

    将字符串按符号分割成数组 var str = 'aa,43,rt,55'; var arr = str.split(/[^0-9a-zA-Z\u4e00-\u9fa5]+/) //arr = [aa ...