AI功能简介:这个AI是在第一期的基础上进行修改后的AI,第一期的AI不能够自动追踪Palyer,只能够停留在原地不动,现在能够去自动追踪主角,只要进入了追踪范围内,就会一直追踪玩家,直至玩家离开追踪范围或被消灭,相关代码如下,操作与第一期相同,第一期链接如下:AI 第一期
展示:

脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bunny : MonoBehaviour
{public static Bunny bunny;private void Awake(){bunny = this;}Animator animator;Rigidbody2D rg;public float MoveSpeed = 0;float changeTime = 0;float time = 0;//随机时间public float minTime = 0;public float maxTime = 0;int direction = 1;float distance_ = 0;bool isChange = false;bool canHit = false;Transform PlayerTransform;public float Distance = 0;float Dwell_gap = 0; //停留时间float Dwell_Cumulative = 0; //停留时间累积public float DwellMinTime = 0;public float DwellMaxTime = 0;float moveTime = 0; //行走时间//行走的最大时间和最短时间设定public float moveMinTime = 0;public float moveMaxTime = 0;float moveTimeCumulative = 0;bool isDwell = false;bool isDwellRange = false;bool CanMove = false;bool CanCount = false;bool CanTimeCumulative = false;bool isAttacking = false;bool isAttacked = false;public bool TrackPlayer = false;public float TrackSpeed = 0;public float CurrentPlayer = 0;public float Life_value = 0;public float hitTime = 0;bool isHit = false;float time1 = 0;public float TrackDistance = 0;GameObject gm;bool isinvincible = false;public float destory_door_value = 0;void Start(){animator = GetComponent<Animator>();rg = GetComponent<Rigidbody2D>();CanMove = false;CanCount = true;}void Update(){PlayerTransform = GameObject.Find("Player").transform;//得到与玩家的距离distance_ = Mathf.Sqrt(Mathf.Abs(PlayerTransform.transform.position.x - transform.position.x) * Mathf.Abs(PlayerTransform.transform.position.x - transform.position.x) +Mathf.Abs(PlayerTransform.transform.position.y - transform.position.y) * Mathf.Abs(PlayerTransform.transform.position.y - transform.position.y));time = time + Time.deltaTime;if (isChange == false){changeTime = Random.Range(minTime, maxTime);isChange = true;}transform.localScale = new Vector3(-direction, 1, 1);if (time > changeTime){direction = -direction;transform.localScale = new Vector3(-direction, 1, 1);time = 0;isChange = false;}//判断是否在攻击距离之内if (distance_ < Distance){canHit = true;}else{canHit = false;}if (canHit&&TrackPlayer){//判断玩家在怪物的哪边if (PlayerTransform.transform.position.x < transform.position.x){direction = 1;animator.SetBool("attack", true);GMF_PlayerController._PlayeController.inJured(CurrentPlayer);transform.localScale = new Vector3(direction, 1, 1);}else{direction = -1;animator.SetBool("attack", true);transform.localScale = new Vector3(direction, 1, 1);GMF_PlayerController._PlayeController.inJured(CurrentPlayer);}}else{animator.SetBool("attack", false);}//随机暂停时间 开始暂停 播放停留动画 人物停止移动 生成暂停时间if (CanCount){isDwellRange = true;CanTimeCumulative = true;CanCount = false;}if (isDwellRange) //设置停留动画 设置暂停时间 {Dwell_gap = Random.Range(DwellMinTime, DwellMaxTime);animator.SetBool("idem", true);animator.SetBool("run", false);CanMove = false;isDwellRange = false;}if (CanTimeCumulative){Dwell_Cumulative = Dwell_Cumulative + Time.deltaTime;}if (Dwell_Cumulative > Dwell_gap) //超过暂停时间 停留结束 结束停留动画 生成行走时间 开始行走动画{animator.SetBool("idem", false);animator.SetBool("run", true);moveTime = Random.Range(moveMinTime, moveMaxTime);CanMove = true;Dwell_Cumulative = 0;CanTimeCumulative = false;}//行走时间结束 if (CanMove){moveTimeCumulative = moveTimeCumulative + Time.deltaTime;if (moveTimeCumulative > moveTime){CanMove = false;moveTimeCumulative = 0;CanCount = true;}}if (Life_value <= 0){Destroy(this.gameObject);}if (isHit){// animator.SetBool("current", true);time1 = time1 + Time.deltaTime;if (time1 > hitTime){animator.SetBool("current", false);isinvincible = false;isHit = false;time1 = 0;}}}private void FixedUpdate(){//没有停留就可以移动if (CanMove){MoveMent();}}void MoveMent(){if (TrackPlayer){if (distance_ < TrackDistance){CanMove = true;Vector2 temp = Vector2.MoveTowards(transform.position, PlayerTransform.position, MoveSpeed * 2 * Time.deltaTime);//考虑到可能有碰撞检测,所以使用刚体的移动方式GetComponent<Rigidbody2D>().MovePosition(temp);//判断玩家在怪物的哪边if (PlayerTransform.transform.position.x < transform.position.x){direction = -1;transform.localScale = new Vector3(direction, 1, 1);}else{direction = 1;transform.localScale = new Vector3(direction, 1, 1);}}else{rg.velocity = new Vector2(1 * direction * MoveSpeed, 0);}}else{rg.velocity = new Vector2(1 * direction * MoveSpeed, 0);}}private void OnTriggerExit2D(Collider2D collision){if (collision.tag == "Player"){CanMove = true;}}private void OnTriggerEnter2D(Collider2D collision){if (collision.tag == "Player"){CanMove = false;}if (collision.tag == "bullte" || collision.tag=="hammer"){if (isinvincible == false){animator.SetBool("current", true);isinvincible = true;isHit = true;}}}public void Current(float value){Life_value = Life_value - value;}}

Unity2D 敌人追踪/攻击/移动AI 第二期相关推荐

  1. 9.Unity2D 横版 简单AI 之 敌人跳跃条件优化+自动范围内检测敌人发起攻击(索敌)+对象池优化+主角受伤死亡

    总目录https://blog.csdn.net/qq_54263076/category_11900070.html?spm=1001.2014.3001.5482 1.动画animation和动画 ...

  2. Unity2D敌人/怪物AI控制 第一期

    AI:原地巡逻自动攻击型 AI会在横版地图上向左向右移动,移动一段距离后会原地停止移动,等待一段时间后,会随机向左或向右移动,以此循环,其中AI移动速度,移动时间.停留时间均可以自行调控,当人物进入怪 ...

  3. 【ChatGPT与网络安全攻击】AI密码破解器可在60秒内攻破50%以上普通密码

    研究表明,ChatGPT等功能强大AI工具已经被用于网络攻击者实施犯罪活动,例如开发恶意软件和生成钓鱼邮件等.如果人们的密码从数据库泄露或被破坏,那么网络攻击者采用AI密码破解器猜出密码是概率几乎是1 ...

  4. unity 敌人自动攻击和寻路_【A*Pathfinding】超级简单的Unity2D寻路

    哈喽~我是yumir. 写过俯视角射击和"元气骑士"地图生成之后,不做个敌人AI总觉得差点什么,所以又研究了一下Unity的2D寻路. 这次我用的是A*Pathfinding的免费 ...

  5. unity自动生成敌人_Unity 3D做2D坦克大战--敌人自动攻击AI编写

    敌人AI攻击方法的编写 老师 | Trigger 学习者 |小白 出品 | Siki 学院 ```java public class Enemy : MonoBehaviour { //属性值 pub ...

  6. Unity 3D做2D坦克大战--敌人自动攻击AI编写

    敌人AI攻击方法的编写 老师 | Trigger 学习者 |小白 出品 | Siki 学院 public class Enemy : MonoBehaviour {//属性值public float ...

  7. unity2D学习(10)创建敌人、为敌人编写简单的AI

    1 创建敌人 按照之前创建Player角色的方法,一样创建出敌人,并为角色添加刚体.碰撞.动画.具体可以参考我之前的unity2D学习(4)(5),具体的细节就不放在这里了. 素材包里面有三种敌人(c ...

  8. 供应链安全、勒索攻击、AI赋能——2022网络安全技术呈何趋势?

    中国信息安全.腾讯安全联合实验室及腾讯研究院共同发布<2022产业安全十大趋势>,在"产业安全宏观态势"."产业安全实践"."产业安全技术 ...

  9. 抵抗敌人的攻击_英雄联盟:很抗打的中单英雄,冲在前排可以抵挡敌人的强势攻击!...

    正义巨像(加里奥)是个很抗打的英雄,在团战中,正义巨像(加里奥)不仅可以抵挡敌人的强势攻击,冲在前排的正义巨像(加里奥),还可以很好的保护己方的后排,让他们保持稳定输出. 目前,在中单英雄排行榜上,正 ...

最新文章

  1. 提高开发效率之安卓模板(上面有四种模板的教程,我之前会两种,看完之后还是只会两种2333)
  2. extra energy theory
  3. oracle 表空间
  4. mnist手写数字识别_手写数字识别
  5. TI Davinci DM6446开发攻略——开发环境搭建
  6. leetcode 633. 平方数之和(双指针)
  7. RabbitMQ实现多系统间的分布式事务,保证数据一致性
  8. python单/双下划线使用
  9. php红盟,php教程_CI框架源码完全分析之核心文件URI.php
  10. 20191130_C6H6(GT)预测
  11. 抓包工具Fiddler基本使用
  12. Yii路由之LimeSurvey去掉烦人的/index.php/*
  13. timestamp显示毫秒_TimeStamp 毫秒和纳秒
  14. java计算机毕业设计教务排课系统源码+mysql数据库+系统+lw文档+部署
  15. 计算机 无法 访问共享网络打印机,共享打印机无法连接怎么办解决方案
  16. 来一起学习脚本语言吧,简单,高效,解放双手,感受自由!
  17. 动环监控设备维护与故障处理,动环监控系统调试
  18. awb数据怎么计算_AWB参数概念
  19. 从 sourcemap 中获取源码
  20. MOOS程序解析记录(6)uSimMarine解析1

热门文章

  1. Idea设置方法注释和类注释
  2. Mac下查看已安装的jdk版本及其安装目录
  3. JavaScript:演示Ajax的get和post请求,练习选顶卡和换肤案例
  4. 员工入职管理系统|员工管理系统|基于SpringBoot+Vue的企业新员工入职系统
  5. java模板引擎 jade_Jade 模板引擎使用
  6. 国内“风口”转变,中国游戏公司纷纷“外逃”,东南亚是个好去处
  7. [转载] 硬件工程师经典笔试题集锦---(张飞实战电子)
  8. 通向实在之路暂记003:双曲几何
  9. NVIDIA AI City Challege 2018资料整理
  10. 九章算法面试题13 随机数生成器