版权申明:
- 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123
- 优梦创客的官方博客:https://91make.top
- 优梦创客的游戏讲堂:https://91make.ke.qq.com
- 『优梦创客』的微信公众号:umaketop
目标
- Tower的Cylinder的放大,防止先打到砖块
- 场景的调色
- 命中后子弹消失
- Tank的调整
- 砖块淡出禁用碰撞体
- Tower没有上升完毕禁止射击
Tower的Cylinder的放大
- 把我们的Cylinder放大至合适的大小(比砖块大一些)即可;
场景的调色
- 首先在我们的场景上找到太阳光(Directional Light)并把颜色参数设置为如下所示:
命中后子弹消失
- 打开我们的Projectile脚本,找到碰撞发生函数,并添加如下代码
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Tower")
{
Destroy(gameObject);
collision.transform.parent.GetComponent<Tower>().DropTower();
}
if(collision.gameObject.tag == "Arch")
{
lober.endPos = player.transform.position;
lober.enabled = true;
player.gameover = true;
Destroy(gameObject, 1.5f);
}
}
Tank的调整
- 打开我们的Level1预制体找到我们的Tank把Tank拖到Level1的子节点,也就是和平台是同一节点即可
砖块淡出禁用碰撞体
- 首先打开我们的方块预制体和圆柱预制体把里面的碰撞体Collider都更改为MeshCollider。
- 打开我们的Bricks脚本找到setFadeout函数添加如下代码即可:
internal void setFadeout()
{
isFadeout = true;
GetComponent<MeshCollider>().enabled = false;
}
Tower没有上升完毕禁止射击
- 打开Tank脚本增加一个Bool变量名为isMovingUp然后在Update函数里编写如下代码
void Update()
{
if (gameover)
return;
if (Input.GetButtonDown("Fire1"))
{
pressTime = Time.time;
}
if (isMovingUp)
return;
if (Input.GetButton("Fire1")&&Time.time - lastFireTime>0.2f)
{
//每一帧计算当前时间-按下时间之差d
float d = Time.time - pressTime;
//实例化一个子弹;
var transProj = Instantiate(Bullet).transform;
float f = d / 2;//最大2秒内有变化,超过2秒使用最高音高系数(1)
if (f > 1)
{
f = 1;
}
transProj.GetComponent<AudioSource>().pitch = 1 + f * 2;
//设置子弹的父节点;
transProj.SetParent(transform, false);
//设置子弹的坐标;
transProj.position = ShotPos.position;
//给子弹一个力,可以让子弹往前移动;
Vector3 v = ShotPos.forward * 20;
transProj.GetComponent<Rigidbody>().AddForce(v, ForceMode.Impulse);
lastFireTime = Time.time;
}
}
public class Tower : MonoBehaviour
{
public int Height;
public Color[] Colors = new Color[2];
public GameObject brickPrefab;
public List<GameObject> bricks = new List<GameObject>();
public bool isRisenUp = false;//砖块是不是全部从地下钻上来了
public Tank tank;
// Start is called before the first frame update
void Start()
{
tank = GameObject.Find("Player").GetComponent<Tank>();
for(int i = 0; i < Height; i++)
{
GameObject goBrick = Instantiate(brickPrefab);
goBrick.transform.SetParent(transform,false );
goBrick.transform.Translate(0, i, 0,Space.Self);
goBrick.GetComponent<MeshRenderer>().material.color = Colors[i % 2];
goBrick.transform.Rotate(0, i*10, 0,Space.Self);
bricks.Add(goBrick);
}
StartCoroutine(RiseUpFormUnderground());
}
IEnumerator RiseUpFormUnderground()
{
transform.Translate(0, -Height * transform.lossyScale.y, 0, Space.Self);
while (transform.position.y<0.8*transform.lossyScale.y)
{
transform.Translate(0, 5 * Time.deltaTime, 0, Space.Self);
yield return null;
}
isRisenUp = true;
tank.isMovingUp = false;
}
// Update is called once per frame
}