zoukankan      html  css  js  c++  java
  • AI 人工智能 探索 (二)

    完整被动技能代码 

    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 () {
        
        }
    }
  • 相关阅读:
    C#中文件操作【File】和【Directory】
    MySql的不同之处
    FileStream读写文件【StreamWriter 和 StreamReader】
    将对象序列化为XML文档
    SQL自增长的数据插入
    POJ3356 AGTC (最短编辑距离问题)
    POJ3070Fibonacci(矩阵快速幂求Fibonacci数列)
    HDOJ1058 Humble Numbers
    第三届软件大赛预赛A组试题及答案
    HDOJ4526 威威猫系列故事——拼车记
  • 原文地址:https://www.cnblogs.com/big-zhou/p/4140232.html
Copyright © 2011-2022 走看看