代码

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour 
{
    
    public GameObject pre;
    public GameObject obj;
    public GameObject node;//记录最近点
    public  GameObject sf ;//self
    private Vector3 v;
    private float time;
    public GameObject[] gos;
    //最近的物体
    private GameObject closeobj;
    private float distance=Mathf.Infinity;
    
    void Start () 
    {
        position=sf.transform.position;
        StartCoroutine(createAnt());
        if(Time.time>=10f)
        StartCoroutine(calculate());
    }
    
    IEnumerator createAnt () 
    {
        InvokeRepeating("initAnt",1f,0.5f);
        yield return new WaitForSeconds(0);
    }
    
    IEnumerator calculate()
    {
        node=calNearNote();
        node.AddComponent("distance");
        //node.GetComponent("distance").
    }
    void initAnt()
    {
        Debug.Log("dodo");
        v=new Vector3(Random.Range(0f,640f),Random.Range(0f,960f),0f);
        //obj=Instantiate(pre,v,Random.rotation);
    }
    // 找到最近的点
    GameObject  calNearNote()
    {
        gos=GameObject.FindGameObjectsWithTag("enemy");
        foreach(GameObject go in gos)
        {
            float curDistance=(go.transform.position-position).sqrMagnitude;
            if(curDistance<distance)
            {
                closeobj=go;
                distance=curDistance;
            }
        }
        return closeobj;
    }
    
}

 

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