Unity3d游戏开发官方入门教程:飞机大战(十)——敌人的爆炸和移动

分类栏目:unity3d教程

143

Unity3d游戏开发官方入门教程:飞机大战(十)——敌人的爆炸和移动

一、创建敌人爆炸特效

为了创建敌人的爆炸特效,将DestroyByContact.cs作出以下改动:

  • 新建一个名为explosion的public对象
  • 在脚本中使用Instantiate()来实例化这个爆炸对象
  • 新建一个名为playerExplosion的public对象
  • 在脚本中使用Instantiate()实例化playerExplosion,并增加tag判断语句,判断是否是Player的tag

DestroyByContact.cs完整代码如下:

using System.Collections; 

using System.Collections.Generic; 

using UnityEngine; 

public class DestroyByContact : MonoBehaviour { 

public GameObject explosion; 

public GameObject playerExplosion; 

//当其他碰撞器进入当前GameObject的触发器时,销毁该碰撞器对应的游戏对象,同时销毁该GameObject 

void OnTriggerEnter(Collider other) { 

if(other.tag == "Boundary") { 

return; 

Instantiate(explosion, transform.position, transform.rotation); 

if(other.tag == "Player") { 

Instantiate(playerExplosion, other.transform.position, other.transform.rotation); } 

Destroy(other.gameObject); 

Destroy(gameObject); } }

接下来,将explosion_asteroid拖拽到explosion和playerExplosion中。并在Player中赋予一个Player的tag。
可见射击后的爆炸效果,以及飞船撞击后的爆炸效果:


二、增加敌人移动脚本

1.将mover.cs移动到Asteroid中,将speed设置为-5。
2.将Asteroid对象拖拽到Prefabs,以便之后批量生成。
3.将Asteroid拖拽出来,能够以-5的速度移动,并实现撞击销毁和飞出边界销毁,效果如下: