程序代码

using UnityEngine;
using System.Collections;

public class calDis : MonoBehaviour 
{

    public GameObject closestAnt,orgi;//orgi is a base point      closeAnt is a sign
    public float  distance;
    public float  ttime=0;
    public GameObject[] gos;
    private GameObject next1,next2,next3,At1,At2,At3;
    private int i1,i2,i3;
    private bool sign=true;
    // Use this for initialization
    void Start () {
        
        orgi=GameObject.Find("Cube") ;    
        
        Debug.Log(orgi);
        
    }
    void Update(){
        ttime+=Time.deltaTime;
        if(ttime>=10f)
        {
          StartCoroutine("calculate");
          ttime=0f;
        }
    }
    IEnumerator calculate()
    {    
        
                gos=GameObject.FindGameObjectsWithTag("Ant");
                yield return StartCoroutine("search1",orgi) ; // one 
                Debug.Log("gggggggggg1");
                Debug.Log(At1.GetInstanceID());
                StopCoroutine("search1");
                yield return StartCoroutine("search2",At1);
                Debug.Log("gggggggggg2");
                Debug.Log(At2.GetInstanceID());
                StopCoroutine("search2");
                yield return StartCoroutine("search3",At2);
                Debug.Log("gggggggggg3");
                Debug.Log(At3.GetInstanceID());
                StopCoroutine("search3");
    }
    
    //cal closest points A
    IEnumerator search1(GameObject clo)
    {
        distance=99999f;
        foreach(GameObject o in gos)
        {
            if(o!=clo)  //culling points
            {
                float curdis=Vector3.Distance(clo.transform.position ,o.transform.position);
                if(curdis<distance)
                {
                    distance=curdis;
                    closestAnt=o;
                }
            }
            
                
        }
        //Debug.Log("Z");'
        At1=closestAnt;
        yield return closestAnt;
        
    }
    
    IEnumerator search2(GameObject clo)
    {
        distance=99999f;
        foreach(GameObject o in gos)
        {
            if(o!=clo&&o!=orgi)  //culling points
            {
                float curdis=Vector3.Distance(clo.transform.position ,o.transform.position);
                if(curdis<distance)
                {
                    distance=curdis;
                    closestAnt=o;
                }
            }
        }
        At2=closestAnt;
        yield return closestAnt;            
    }
    
    IEnumerator search3(GameObject clo) 
    {
        distance=99999f;
        bool findObject=false;
        foreach(GameObject o in gos)
       {
        if(o!=clo&&o!=orgi&&o!=At1) //culling points
         {
                float curdis=Vector3.Distance(clo.transform.position ,o.transform.position);
                if(curdis<distance)
                  {
                        distance=curdis;
                        closestAnt=o;
                        findObject=true;
                  }
         }
       }
        if(!findObject)
         { 
            yield return null;
            Debug.Log("no one for 3");
        }
        else
        {
        At3=closestAnt;
        Debug.Log("ddddd"+At3);
        yield return closestAnt;
        }
    }
    
    
    
    
 

}

 

posted @ 2013-11-14 22:28  小薇林  阅读(216)  评论(0编辑  收藏  举报