AI 人工智能 探索 (二)
2014-12-03 15:35
141 查看
完整被动技能代码
using UnityEngine; using System.Collections; public class AI : MonoBehaviour { private Hashtable table; void Start() { table = new Hashtable(); } void Update() { } //离开碰撞 void OnTriggerExit(Collider other) { //如果消失 if (other.transform.name == otherName) { touch = false; otherName = ""; } table.Remove(other.transform.name); StartCoroutine(Independent(0.1f)); } //多物体碰撞 ,当移除后,必须检测已有碰撞中是否还有符合条件的 IEnumerator Independent(float i) { if (touch == false) //没碰 { foreach (DictionaryEntry de in table) { //检测碰撞,发现导入方法 //加入 Transform transform = de.Value as Transform; if (OnDetection(transform)) { otherName = transform.name; touch = true; } } } yield return new WaitForSeconds(i);//n秒执行一次 遍历,提高效率 } private bool touch = false;//和目标物体发生碰撞没有 private string otherName;//目标物体 //进入碰撞 void OnTriggerEnter(Collider other) { table.Add(other.transform.name, other.transform); if (this.transform.GetComponent<Attribute>().att == 2) { //测试用 print(other.transform.name); print(table.Count); } if (touch == false) //没碰 { foreach (DictionaryEntry de in table) { //检测碰撞,发现导入方法 //加入 Transform transform = de.Value as Transform; if (OnDetection(transform)) { otherName = other.transform.name; touch = true; } } } } //检测碰撞 bool OnDetection(Transform tr) { if (tr.name != transform.name)//碰点不是自己 { //这里写方法判断逻辑 if (tr.GetComponent<Attribute>().att != this.transform.GetComponent<Attribute>().att)//不同属性对打,相同属性 不打 { return true; } else {//重新选择敌人 return false; } } return false; } //逗留碰撞 void OnTriggerStay(Collider other) { if (other.transform.name == otherName) { //检测距离 float distance = Vector3.Distance(this.transform.position, other.transform.position);//距离公式 //根据距离 发射子弹, if (this.transform.name == "momo(Clone)001")//测试用 { // this.transform.LookAt(other.transform); print(this.transform.name + "发射" + otherName); } } } }
using UnityEngine; using System.Collections; public class Attribute : MonoBehaviour { public string name; public int blood;//血 public int speed;//移动速度 public int aggressivity;//攻击力 public int att;//属性 void Start () { } void Update () { } }
相关文章推荐
- AI 人工智能 探索 (八)
- AI 人工智能 探索 (四)
- 深圳云栖大会人工智能专场:探索视频+AI,玩转智能视频应用
- 深圳云栖大会人工智能专场:探索视频+AI,玩转智能视频应用
- AI 人工智能 探索 (五)
- 深圳云栖大会人工智能专场:探索视频+AI,玩转智能视频应用
- AI 人工智能 探索 (六)
- AI 人工智能 探索 (七)
- AI 人工智能 探索 (九)
- WISE大会:涂鸦智能“AI时代 传统制造业将加速探索人工智能”
- AI 人工智能 探索 (十)
- 全球AI报告:探索人工智能革命
- 深圳云栖大会人工智能专场:探索视频+AI,玩转智能视频应用
- AI 人工智能 探索 (一)
- AI 人工智能 探索 (三)
- 深圳云栖大会人工智能专场:探索视频+AI,玩转智能视频应用
- Python语言在人工智能(AI)中的优势
- 深度报告 | AI新职位“人工智能训练师”
- AI一分钟 | 富士康押宝人工智能,将投资21亿元用于AI研发
- 达沃斯的AI思想交锋后,下一个实用人工智能风向标在哪?