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 () {
    
    }
}

 

posted on 2014-12-03 15:35  太粗难进  阅读(274)  评论(0编辑  收藏  举报