27783人加入学习
(125人评价)
如何制作塔防游戏(基于Unity5.5)
价格 免费
using System.Collections;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
    public static int CountEnemyAlive = 0;
    public Wave[] waves;
    public Transform START;
    public float waveRate = 0.2f;

    private void Start()
    {
        StartCoroutine(SpawnEnemy());
    }

    IEnumerator SpawnEnemy()
    {
        foreach (Wave wave in waves)
        {
            for (int i = 0; i < wave.count; i++)
            {
                GameObject.Instantiate(wave.enemyPrefab, START.position, Quaternion.identity);
                CountEnemyAlive++;
                if (i != wave.count - 1)
                    yield return new WaitForSeconds(wave.rate);
            }
            while(CountEnemyAlive>0)
            {
                yield return 0;
            }
            yield return new WaitForSeconds(waveRate);
        }
    }
}
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed = 10.0f;
    private Transform[] positions;
    private int index = 0;

    void Start()
    {
        positions = Waypoints.positions;
    }

    void Update()
    {
        Move();
    }

    private void Move()
    {
        if (index > positions.Length - 1) return;
        transform.Translate((positions[index].position - transform.position).normalized * speed * Time.deltaTime);
        if (Vector3.Distance(positions[index].position, transform.position) < 0.2f)
        {
            index++;
        }
        if(index > positions.Length - 1)
        {
            ReachDestination();
        }
    }

    private void ReachDestination()
    {
        GameObject.Destroy(this.gameObject);
    }

    private void OnDestroy()
    {
        EnemySpawner.CountEnemyAlive--;
    }
}

 

[展开全文]
遗失的星空 · 2021-04-03 · 该任务已被删除 0
using UnityEngine;

//保存每一波敌人生成所需要的属性
[System.Serializable]
public class Wave
{
    public GameObject enemyPrefab;
    public int count;
    public float rate;
}
using System.Collections;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
    public Wave[] waves;
    public Transform START;
    public float waveRate = 3.0f;

    private void Start()
    {
        StartCoroutine(SpawnEnemy());
    }

    IEnumerator SpawnEnemy()
    {
        foreach (Wave wave in waves)
        {
            for (int i = 0; i < wave.count; i++)
            {
                GameObject.Instantiate(wave.enemyPrefab, START.position, Quaternion.identity);
                if (i != wave.count - 1)
                    yield return new WaitForSeconds(wave.rate);
            }
            yield return new WaitForSeconds(waveRate);
        }
    }
}

序列化的类才能显示在检视面板上。

[展开全文]
遗失的星空 · 2021-04-03 · 该任务已被删除 0
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed = 10.0f;
    private Transform[] positions;
    private int index = 0;

    void Start()
    {
        positions = Waypoints.positions;
    }

    void Update()
    {
        Move();
    }

    private void Move()
    {
        if (index > positions.Length - 1) return;
        transform.Translate((positions[index].position - transform.position).normalized * speed * Time.deltaTime);
        if (Vector3.Distance(positions[index].position, transform.position) < 0.2f)
        {
            index++;
        }
    }
}

 

[展开全文]
遗失的星空 · 2021-04-02 · 该任务已被删除 0
using UnityEngine;

public class Waypoints : MonoBehaviour
{
    public static Transform[] positions;

    private void Awake()
    {
        positions = new Transform[transform.childCount];
        for (int i = 0; i < positions.Length; i++)
        {
            positions[i] = transform.GetChild(i);
        }
    }
}

 

[展开全文]
遗失的星空 · 2021-04-01 · 该任务已被删除 0
using UnityEngine;

public class ViewController : MonoBehaviour
{
    public float speed = 25.0f;
    public float mouseSpeed = 600.0f;

    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        float mouse = Input.GetAxis("Mouse ScrollWheel");
        transform.Translate(new Vector3(h * speed, mouse * mouseSpeed, v * speed) * Time.deltaTime, Space.World);
    }
}

 

[展开全文]
遗失的星空 · 2021-04-01 · 该任务已被删除 1

菜单栏GameObject中的Break Prefab Instance ,脱离与Prefab的关联。

菜单栏GameObject下的Clear Parent,可以脱离父子关系。

[展开全文]
遗失的星空 · 2021-04-01 · 该任务已被删除 0

Ctrl + D 复制

按住Ctrl拖动,1米间隔

[展开全文]
遗失的星空 · 2021-04-01 · 该任务已被删除 0

[System.Serializable]设置为可序列化属性

[展开全文]
脑残的中二少年 · 2021-03-28 · 该任务已被删除 0

Horizontal(水平按键)

Vertical(垂直按键)

Space.World(按照世界坐标移动)

[展开全文]
脑残的中二少年 · 2021-04-13 · 该任务已被删除 0

Enemy Spawner

[展开全文]
拉普拉斯妖 · 2020-10-16 · 该任务已被删除 0

public static private

[展开全文]
zhike · 2020-07-08 · 该任务已被删除 0

小Bug:敌人孵化器脚本中的static变量在场景重载时会变-1;

public static int countEnemyAlive = 0;

修复private void Awake()
    {
        countEnemyAlive = 0;
    }

[展开全文]
黑夜007 · 2020-06-30 · 该任务已被删除 0

直接替换材质球:effect.GetComponent<Renderer>().material = materialSelf;

[展开全文]
黑夜007 · 2020-06-27 · 该任务已被删除 0

    void UpdateEnemys()
    {
        enemys.RemoveAll(n => n == null);
    }

搞定。

[展开全文]
黑夜007 · 2020-06-27 · 该任务已被删除 0

设置Physic碰撞矩阵即可。

[展开全文]
黑夜007 · 2020-06-26 · 该任务已被删除 0

public static Transform[] position;

 

positions = new Transform[transform.childCount];

for (int i = 0; i < positionLength; i++){

 

}

[展开全文]
云天coco · 2020-05-07 · 该任务已被删除 0

coroutine

[展开全文]
WayneZ. · 2020-04-05 · 该任务已被删除 0

添加事件时注意选上面的dynamic bool。

 

[展开全文]
WayneZ. · 2020-04-01 · 该任务已被删除 0

OnDestroy 会在 GameObject.Destroy(this.gameObject); 自动被调用。

[展开全文]
WayneZ. · 2020-03-27 · 该任务已被删除 0

要看懂:38

[展开全文]
兒吞 · 2020-03-04 · 该任务已被删除 0