四。控制隕石的生成和控制發射和爆炸聲音
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,添加兩個音樂片段,運行,便可以聽到音樂了