
using UnityEngine;
public class Bomb : MonoBehaviour
{
public GameObject center, side, end;//中心,四边,端。因为炸弹可以无限延长。
public int range;//范围
public float timer;//倒计时
private void OnTriggerExit2D(Collider2D other) {//炸弹生成时是和player重合的
if(other.CompareTag("Player")) {
gameObject.GetComponent<Collider2D>().isTrigger = false;
}
}
private void Update() {
timer -= Time.deltaTime;
if(timer < 0) {
Instantiate(center, transform.position, Quaternion.identity);//实例炸弹爆炸后的火焰,没有用对
象池,暂时不太熟练。
Extend(Vector3Int.up);//根据范围延申向上的火焰
Extend(Vector3Int.down);//向下的
Extend(Vector3Int.left);//向左的
Extend(Vector3Int.right);//因为很快,所以这样写应该没啥问题。
Destroy(gameObject);//炸弹实体销毁,同样没有用到对象池目前。
}
}
private void Extend(Vector3Int dir) {//形参是dir,方向
Vector3 pos = new Vector3();
Quaternion quaternion = new Quaternion();
for(int i = 1; i <= range; i++) {//根据范围延长
pos = transform.position + dir*i;//获得炸弹在当前位置向形参方向一个单位或者几个单位的位置
quaternion = Quaternion.LookRotation(Vector3.forward, -dir);//边的预制体只有一个,在延申向不
//同方向时需要旋转,至于为啥dir带个符号,没有为啥,试出来的。其实当形参是Vector3.Up时,生成的火焰
//是不是向上的,我也不清楚
if(i == range || Physics2D.BoxCast(pos, new Vector2(0.1f, 0.1f), 0, Vector2.zero, 0)) {
Instantiate(end, pos, quaternion);//如果范围到头了或者撞墙了,实例化端
return;
} else {
Instantiate(side, pos, quaternion);//否则实例化边
}
}
}
}
0o0oo