bj
using UnityEngine; using System.Collections; public class node : MonoBehaviour { public int index; public GameObject target;// 手按的物体 public Vector3 v; public GameObject pre,obj; public GameObject closeobj; public GameObject[] gos; public createAnt ca; private float distance=99999; // Use this for initialization void Start () { pre=GameObject.FindGameObjectWithTag("Cube"); StartCoroutine(createAnts); if(Time.time>=10.0f) { StartCoroutine(); } } IEnumerator Cal() { gos=GameObject.FindGameObjectsWithTag("enemy"); CalCulate(); closeobj.AddComponent(); //加脚本 } void CalCulate() { foreach (GameObject o in gos) { float curDis=Vector3.Distance(target.transform.position,o.transform.position); if(curDis<distance) { distance=curDis; closeobj=o; } } return closeobj; } IEnumerator createAnts() { InvokeRepeating("createant",1f,0.3f); } void createant() { v=new Vector3(Random.Range(0f,640f),Random.Range(0f,960f),0f); obj=Instantiate(pre,v,Random.rotation); } }