Unity-3d Day04 自动出兵地图

塔造兵的代码:有三个预设体,通过tag区分

using UnityEngine;
using System.Collections;

public class NewbatScript : MonoBehaviour {
    float time = 0;
    public GameObject batmanpre0;
    public GameObject batmanpre1;
    public GameObject batmanpre2;
    public GameObject batman0;
    public GameObject batman1;
    public GameObject batman2;
    // Use this for initialization
    void Start () {
        
    }
    
    //Update is called once per frame
    void Update () {
        time += Time.deltaTime;
        if (time > 1) {
            batman0 = Instantiate(batmanpre0, new Vector3(20f, 1.2f, 17f), Quaternion.identity) as GameObject;
            batman1 = Instantiate(batmanpre1, new Vector3(17f, 1.2f, 17f), Quaternion.identity) as GameObject;
            batman2 = Instantiate(batmanpre2, new Vector3(17f, 1.2f, 20f), Quaternion.identity) as GameObject;
            time = 0;
        }
    }
}

兵分路和移动的代码:

using UnityEngine;
using System.Collections;

public class BatmanScript : MonoBehaviour {
    public GameObject[,] tower = new GameObject[3, 4];
    public GameObject target;
    public int road;
    public int num;
    public GameObject home;
    // Use this for initialization
    void Start () {
        tower[0, 0] = GameObject.Find("Tower00");
        tower[0, 1] = GameObject.Find("Tower01");
        tower[0, 2] = GameObject.Find("Tower02");
        tower[0, 3] = GameObject.Find("Tower03");
        tower[1, 0] = GameObject.Find("Tower10");
        tower[1, 1] = GameObject.Find("Tower11");
        tower[1, 2] = GameObject.Find("Tower12");
        tower[1, 3] = GameObject.Find("Tower13");
        tower[2, 0] = GameObject.Find("Tower20");
        tower[2, 1] = GameObject.Find("Tower21");
        tower[2, 2] = GameObject.Find("Tower22");
        tower[2, 3] = GameObject.Find("Tower23");
        road = int.Parse(transform.tag);
        num = 0;
        target = tower[road, num];
        home = GameObject.Find("Home1");
    }
    // Update is called once per frame
    void Update () {
        if (Vector3.Distance(transform.position, target.transform.position) < 3)
        {
            if (num < 3)
            {
                num += 1;
                target = tower[road, num];
            }
            else {
                target = GameObject.Find("Home1");
            }
        } 

        transform.LookAt(target.transform.position);
        transform.Translate(Vector3.forward/6);

        
    }

}

兵与对面水晶碰撞自动销毁:

using UnityEngine;
using System.Collections;

public class Home1Script : MonoBehaviour {
    // Use this for initialization
    void Start () {
      
    }
    void OnCollisionEnter(Collision collision) {
        Destroy(collision.gameObject);
    }
    // Update is called once per frame
    void Update () {
    
    }
}

效果图:地图就是一个cube贴上的图片   吼吼  nice

原文地址:https://www.cnblogs.com/little-sun/p/4370354.html