塔防篇:Day3

来源:互联网 发布:qq三国JS单挑77boss 编辑:程序博客网 时间:2024/05/16 15:06

Morning:

初步目标: 子弹效果。Go!

一: 给Enemy创建敌人标签, List 容器实现存储,给炮台添加刚体以及触发器检测。

碰撞检测: 优化性能,添加Turret Enemy Layer。Edit-》 project Setting->physics 


实例化子弹: 添加一个球体,材质做成金属,再给炮台添加脚本 

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Turret : MonoBehaviour {public List<GameObject> enemys=new List<GameObject>();void OnTriggerEnter(Collider col){if (col.tag == "Enemy") {enemys.Add (col.gameObject);}}void OnTriggerExit(Collider col){if (col.tag == "Enemy") {enemys.Remove (col.gameObject);}}public int attackRateTime = 1;//攻击间隔private float timer=0;public GameObject bulletPrefab;public Transform firePosition;void Start () {timer = attackRateTime;}void Update(){timer += Time.deltaTime;if (enemys.Count>0&&timer >= attackRateTime) {Attack ();timer -= attackRateTime;}}void Attack(){GameObject.Instantiate (bulletPrefab,firePosition.position,firePosition.rotation);}}



二:接下来控制 ,子弹去追踪敌人,添加子弹AI:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Bullet : MonoBehaviour {public int damage=50;public float speed=70000;private Transform target;// Use this for initializationvoid Start () { }public void SetTarget(Transform _target){this.target = _target;}void Update(){transform.LookAt (target.position);transform.Translate (Vector3.forward*speed);}}

。。。上午被老师叫去听课了。。卒。。


下午回来:

检查了下上午代码发现一个bug... 无论speed权重多大,U3D显示效果都那样,检查之后发现,以后如果碰到public 首先要在inspector中检测他的初值。

inspector中的初值和脚本中初值不一样,被定死了,所以怎么改脚本数值内容都不影响效果。

给子弹加爆炸特效 

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Bullet : MonoBehaviour {public int damage=50;private float speed=200f;private Transform target;public GameObject explosionEffectPrefab;void Start () { }public void SetTarget(Transform _target){this.target = _target;}void Update(){transform.LookAt (target.position);transform.Translate (Vector3.forward*speed*Time.deltaTime);//1 }void OnTriggerEnter(Collider col){if (col.tag == "Enemy") {col.GetComponent<Enemy1> ().TakeDamage (damage);GameObject effect=GameObject.Instantiate (explosionEffectPrefab,transform.position,transform.rotation);GameObject.Destroy (this.gameObject);Destroy (effect, 1);}} void OnDestroy(){}}

效果还是蛮秀的,爆炸效果可以自己拿粒子系统简单的调一调

接下来修复bug.

bug: 当敌人到达终点,空指针异常, list不出链表,以及子弹找不到目标,

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Bullet : MonoBehaviour {public int damage=50;private float speed=150f;private Transform target;public GameObject explosionEffectPrefab;void Start () { }public void SetTarget(Transform _target){this.target = _target;}void Update(){if (target == null) {Die ();return;}transform.LookAt (target.position);transform.Translate (Vector3.forward*speed*Time.deltaTime);//1 }void OnTriggerEnter(Collider col){if (col.tag == "Enemy") {col.GetComponent<Enemy1> ().TakeDamage (damage);Die ();}} void Die(){GameObject effect=GameObject.Instantiate (explosionEffectPrefab,transform.position,transform.rotation);GameObject.Destroy (this.gameObject);Destroy (effect, 1);}void OnDestroy(){}}

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Turret : MonoBehaviour {public List<GameObject> enemys=new List<GameObject>();void OnTriggerEnter(Collider col){if (col.tag == "Enemy") {enemys.Add (col.gameObject);}}void OnTriggerExit(Collider col){if (col.tag == "Enemy") {enemys.Remove (col.gameObject);}}public int attackRateTime = 1;//攻击间隔private float timer=0;public GameObject bulletPrefab;public Transform firePosition;void Start () {timer = attackRateTime;}void Update(){ timer += Time.deltaTime;if (enemys.Count>0&&timer >= attackRateTime) {Attack ();timer = 0;}}void Attack(){if (enemys[0]==null) {UpdateEnemys ();}if (enemys.Count > 0) {GameObject bullet = GameObject.Instantiate (bulletPrefab, firePosition.position, firePosition.rotation);bullet.GetComponent<Bullet> ().SetTarget (enemys [0].transform);} else {timer = attackRateTime;}}void UpdateEnemys(){ List<int> emptyIndex = new List<int> ();for (int i = 0; i < enemys.Count; i++) {if (enemys [i] == null)emptyIndex.Add (i);}for (int i = 0; i < emptyIndex.Count; i++) {enemys.RemoveAt (emptyIndex [i]-i);}}}

这样就消除bug了。方法是,检测LIST链表中null元素,将之清除

明天继续。

原创粉丝点击