四。控制陨石的生成和控制发射和爆炸声音
1.陨石的生成。新建一个名为EnemySpawnPosition的空物体,位置和旋转角度如下
2.在随便创建一个空物体,取名为M_GameManager,新建一个脚本也是这个名字,用来处理陨石生成的算法,作为游戏控制器存在
3.打开M_GameManager脚本,编辑如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class M_GameManager : MonoBehaviour {
public Transform EnemySpawnPosition;//生成点的基础位置
private float SpawnDurationTime = 2;//生成的间隔时间
public GameObject Enemy01;//生成的敌人预制体
void Start () {
}
void Update () {
CreatEnemy();
}
private void CreatEnemy()//生成的方法
{
SpawnDurationTime -= Time.deltaTime;
if (SpawnDurationTime <= 0)
{
Instantiate(Enemy01, new Vector3(GetRandomPos(-4, 4), EnemySpawnPosition.position.y, EnemySpawnPosition.position.z), EnemySpawnPosition.rotation);
SpawnDurationTime = 2.0f;
}
}
private float GetRandomPos(float Min,float Max)//生成随机数的方法
{
return Random.Range(Min, Max);
}
}
回到unity把生成点和预制体挂上,运行便发现可以生成物体。
4.添加三个声音,一个是发射子弹的声音,一个是陨石爆炸的声音,一个是玩家爆炸的声音
所以我们分别在玩家和陨石上挂上音频组件,如下设置
5.打开陨石的脚本,也就是Enemy脚本,加上并且修改以下代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum EnemyType//敌人的类型
{
Simple,
Middle,
Higher,
}
public class Enemy : MonoBehaviour {
public EnemyType enemyType;//类型
public float MoveSpeed;//移动速度
public GameObject ExplosionVFX;//陨石爆炸粒子
private AudioSource ExplosionAS;//陨石爆炸的音频组件
void Start () {
ExplosionAS = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
DealWithEnemyType();
}
public void DealWithEnemyType()//处理敌人类型的各种操作,敌人类型不同,攻击运动方式也不一样
{
switch (enemyType)
{
case EnemyType.Simple:
SimpleEnemyMoveMent();
break;
case EnemyType.Middle:
break;
case EnemyType.Higher:
break;
default:
break;
}
}
private void SimpleEnemyMoveMent()
{
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Bullet")//与子弹进行碰撞
{
if (!ExplosionAS.isPlaying)//判断是否在播放
{
ExplosionAS.Play();
}
Destroy(other.gameObject);//销毁子弹
GameObject ex = Instantiate(ExplosionVFX, transform.position, Quaternion.identity);//生成爆炸粒子
Destroy(gameObject, 0.15f);//销毁本体
Destroy(ex, 0.15f);//销毁爆炸粒子
}
}
}
如果仔细看会发现音频播放下面一段与之前的不一样,因为这里当时写的太快,忘记了这个小Bug,就是如果立马销毁本体物体,音频组建就不可能播放完,所以这里要在0.15秒后进行销毁才行。
6,打开玩家脚本,添加和修改如下代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]//可显示
public class PlayerClampBounder//移动的X,Z最大范围或者最小范围类
{
public float MaxX;
public float MaxZ;
public float MinX;
public float MinZ;
}
public class Player : MonoBehaviour {
public PlayerClampBounder Bounder;//边界范围
public float MoveSpeed;//移动速度
public float ShotDuratime = 0.3f;//发射的间隔时间
public GameObject BulletPrefab;//子弹的预制体,在unity进行赋值
private Transform BulletPoint;//子弹生成的位置
public GameObject PlayerExplosion;//玩家爆炸
private AudioSource PlayerAS;//玩家的声音组件
public AudioClip PlayerExplosionClip;//玩家爆炸的音频
public AudioClip PlayerShootClip;//玩家发射子弹的音频
void Start () {
BulletPoint = transform.Find("BulletPoint");//获取子物体BulletPoint
PlayerAS = GetComponent<AudioSource>();//获取音频组件
}
// Update is called once per frame
void Update () {
MoveMent();
ShotBullet();
}
public void MoveMent()//控制移动的方法
{
float Horizontal = Input.GetAxis("Horizontal");//获取水平输入轴
float Vertical = Input.GetAxis("Vertical");//获取垂直输入轴
if (transform.position.x >= Bounder.MaxX && Horizontal > 0)//如果当前x位置大于等于边界范围,且还在向这个方向运动,也就是说还按下了向右,就让水平输入值为0,也就不会再继续向右
{
Horizontal = 0;
}
else if (transform.position.x <= Bounder.MinX && Horizontal < 0)//同理
{
Horizontal = 0;
}
else if (transform.position.z >= Bounder.MaxZ && Vertical > 0)
{
Vertical = 0;
}
else if (transform.position.z <= Bounder.MinZ && Vertical < 0)
{
Vertical = 0;
}
transform.Translate(Horizontal*MoveSpeed*Time.deltaTime, 0, Vertical*Time.deltaTime*MoveSpeed);//移动
}
private void ShotBullet()
{
ShotDuratime -= Time.deltaTime;
if (ShotDuratime <= 0&&Input.GetMouseButton(0))//当时间到了且按下了鼠标左键
{
if (!PlayerAS.isPlaying)
{
PlayerAS.clip = PlayerShootClip;
PlayerAS.Play();
}
Instantiate(BulletPrefab, BulletPoint.position, Quaternion.identity);//生成子弹
ShotDuratime = 0.3f;//重新设置间隔时间
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")//碰到敌人
{
if (!PlayerAS.isPlaying)
{
PlayerAS.clip = PlayerExplosionClip; ;
PlayerAS.Play();
}
Destroy(other.gameObject);//销毁敌人
GameObject obj = Instantiate(PlayerExplosion, transform.position, Quaternion.identity);//生成爆炸物
Destroy(obj, 0.3f);//销毁爆炸物
Destroy(gameObject,0.2f);//销毁自己
}
}
}
这里呢是用一个音频组件播放两个音乐片段,实现起来也很简单。
回到unity,添加两个音乐片段,运行,便可以听到音乐了