塔防篇:Day1

来源:互联网 发布:iphone6连不上移动数据 编辑:程序博客网 时间:2024/06/05 02:05

Morning:

地图初步构建,买了份淘宝材料。。地图比较正,然后搭满小方格,确定好初始和结束为止,再调节摄像机初始值。

准备使摄像机可以动态移动。

摄像机脚本处问题:

因为摄像机初始位置旋转了一定的角度,所以 普通的 Translate无法正常前后左右平移。需要加上space.world,滚轮控制上下,似乎有点慢,乘上些数字在inspector面板自己搞搞。

 

using System.Collections;using System.Collections.Generic;using UnityEngine;public class CameraController : MonoBehaviour { public float speed = 1;public float mouseSpeed = -10.0f;// Update is called once per framevoid Update () {float h = Input.GetAxis ("Horizontal");float v = Input.GetAxis ("Vertical");float mouse = Input.GetAxis ("Mouse ScrollWheel");transform.Translate(new Vector3(h,mouseSpeed*mouse,v)*speed,Space.World);}}

接下来就是敌人走的路径了...学了两年算法本来想搞个Astar扔上去。。然而并不会将地图数据转化为程序坐标数据,这岂不是很气。。所以造了tag关键点。标记敌人路径。


打了标记。。感觉丑的不行。。完全没算法的那种感觉。。不过还是能实现需要的效果的。。。强行自我安慰。。。开个static变量,好让敌人可以访问到下次怎么走。 

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Waypoints : MonoBehaviour {// Use this for initializationpublic static Transform[] positions;void Awake(){positions = new Transform[transform.childCount];for (int i = 0; i < positions.Length; i++) {positions [i] = transform.GetChild (i);}} }
接下来控制敌人行走。得到index脚标

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Enemy1 : MonoBehaviour {private Transform[] positions;private int index = 0;public float speed=10.0f;// Use this for initializationvoid Start () {positions = Waypoints.positions; }// Update is called once per framevoid Update () {Move ();}void Move(){Debug.Log ( (positions[index].position-transform.position).normalized*speed);transform.Translate ( (positions[index].position-transform.position).normalized *speed);if (Vector3.Distance (positions [index].position, transform.position) <  2f)index++;}}

之后建立敌人的一个孵化器。序列化一下每一波敌人的参数。再用prefab填充,可以自动控制间隔。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class EnemySpwaner : MonoBehaviour {public  Wave[] waves;public Transform START;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);yield return new WaitForSeconds (wave.rate);}}}}
using System.Collections;using System.Collections.Generic;using UnityEngine;//保存每一波敌人生成属性[System.Serializable]//可以被序列化的public class Wave  {public GameObject enemyPrefab;public int count;public float rate;}

附上初步面板配置:

上午结束。初步效果图:


afternoon:

上完网络课,回来补了下敌人产生优化。

之前因为做的会出现一个问题,上一波敌人刚出现还没到终点,下一波又来。

所以在协程中加了个当前存活个数,如果存活个数>0 就让这个协程一直等待,当敌人被摧毁或者到达终点个数减一。

using System.Collections;using System.Collections.Generic;using UnityEngine;public class EnemySpwaner : MonoBehaviour {public  Wave[] waves;public Transform START;public float waveRate=3.0f;public static int CountEnemyAlive = 0;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 System.Collections;using System.Collections.Generic;using UnityEngine;public class Enemy1 : MonoBehaviour {private Transform[] positions;private int index = 0;public float speed=10.0f;// Use this for initializationvoid Start () {positions = Waypoints.positions; }// Update is called once per framevoid Update () {Move ();}void Move(){ transform.Translate ( (positions[index].position-transform.position).normalized *speed);if (Vector3.Distance (positions [index].position, transform.position) <  2f)index++;if (index > positions.Length - 1) {ReachDesination ();}}void ReachDesination(){GameObject.Destroy (this.gameObject);}void OnDestroy(){EnemySpwaner.CountEnemyAlive--;Debug.Log (EnemySpwaner.CountEnemyAlive);}}

顺便把炮台prefab更新了下。明天继续。



原创粉丝点击