【Unity2D入门教程】简单制作战机弹幕射击游戏⑥最终回扩展其它范围的内容
生活随笔
收集整理的這篇文章主要介紹了
【Unity2D入门教程】简单制作战机弹幕射击游戏⑥最终回扩展其它范围的内容
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
制作分數(shù)和生命的UI:
由于我們前面沒有做類似的UI所以這里教大伙一下基本思路:
首先我們創(chuàng)建一個canvas用來創(chuàng)建兩個Text用來顯示分數(shù)和生命的UI
藍色的是分數(shù)黃色的是生命
我們創(chuàng)建一個scoreplay的腳本掛載在text上
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScoreDisplay : MonoBehaviour {Text scoreText;GameSeesion gameSession;private void Start(){scoreText = GetComponent<Text>();gameSession = FindObjectOfType<GameSeesion>();}private void Update(){scoreText.text = gameSession.GetScore().ToString();} }創(chuàng)建一個空對象gamesession還有一個同名腳本掛載在它上面:
接著我們改一下Player和Enemy的腳本
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Player : MonoBehaviour {[Header("玩家移動")][SerializeField] float ySpeed = 10f;[SerializeField] float xSpeed = 10f;[SerializeField] float padding = 1f;[Header("Play Health")][SerializeField] int health = 500;[Header("ProjectTile")][SerializeField] GameObject laserPrefab;[SerializeField] float projectTileSpeed = 10f;[SerializeField] float projectTileFiringPeriod = 0.1f;//戰(zhàn)機在屏幕能移動的坐標(biāo)float xMin;float xMax;float yMin;float yMax;//協(xié)程的編程是指在不堵塞主線程的情況下執(zhí)行某些特定的函數(shù)Coroutine fireCoroutine;[SerializeField] AudioClip deathSFX;[SerializeField] [Range(0, 1)] float deathSoundVolume = 0.75f;[SerializeField] AudioClip shootSFX;[SerializeField] [Range(0, 1)] float shootSoundVolume = 0.55f;void Start(){SetUpMoveBoundaries();}private void SetUpMoveBoundaries(){Camera gameCamera = Camera.main;//之前的視頻說過,Camera.main.ViewportToWorldPoint()是將攝像機視角的坐標(biāo)轉(zhuǎn)化為世界坐標(biāo)然后padding是防止戰(zhàn)機出屏幕邊緣xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;}void Update(){Move();Fire();}private void Move(){//Input Manager上兩個監(jiān)控鍵盤上WSAD按鍵而生成-1到1值的var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * ySpeed;var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * xSpeed;// Debug.Log(deltaX);//限制移動范圍var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);transform.position = new Vector2(newXPos,newYPos);}private void Fire(){if (Input.GetButtonDown("Fire1")){fireCoroutine = StartCoroutine(FireContinuously());}if (Input.GetButtonUp("Fire1"))//這個Fire1也是Input Manager上的{StopCoroutine(fireCoroutine); //暫停某個協(xié)程// StopAllCoroutines();}}//協(xié)程函數(shù)是用關(guān)鍵字迭代器IEnumerator而且一定要用yield關(guān)鍵詞返回IEnumerator FireContinuously(){while (true){GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject; //生成子彈laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectTileSpeed); //給子彈一個向上的力AudioSource.PlayClipAtPoint(shootSFX, Camera.main.transform.position, shootSoundVolume);yield return new WaitForSeconds(projectTileFiringPeriod); //下一顆子彈發(fā)生的間隔時間}}private void OnTriggerEnter2D(Collider2D other){DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();if (!damageDealer) { return; }ProcessHit(damageDealer);}private void ProcessHit(DamageDealer damageDealer){health -= damageDealer.GetDamage(); //減去收到的傷害damageDealer.Hit();if (health <= 0){Die();}}public int GetHealth(){return health;}private void Die(){Destroy(gameObject); //生命值為小于等于0就銷毀AudioSource.PlayClipAtPoint(deathSFX, Camera.main.transform.position, deathSoundVolume);} }?
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Enemy : MonoBehaviour {[Header("Enemy States")][SerializeField] float health = 100f;[SerializeField] int scoreValue = 150;[Header("Shooting")][SerializeField] float shotCounter;[SerializeField] float minTimeBetweenShots = 0.2f;[SerializeField] float maxTimeBetweenShot = 1.5f;[SerializeField] GameObject projecttile;[SerializeField] float projecttileSpeed = 10f;[Header("Sound Effects")][SerializeField] GameObject deathDFX;[SerializeField] AudioClip deathSFX;[SerializeField] [Range(0,1)]float deathSoundVolume = 0.75f;[SerializeField] AudioClip shootSFX;[SerializeField] [Range(0, 1)] float shootSoundVolume = 0.50f;void Start(){shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShot);}void Update(){CountDownAndShoot();}private void CountDownAndShoot(){shotCounter -= Time.deltaTime; //計時器,用來當(dāng)計時器小于等于0時重置發(fā)射時間并執(zhí)行發(fā)射函數(shù)if(shotCounter <= 0){Fire();shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShot);}}private void Fire(){GameObject laser = Instantiate(projecttile, transform.position, Quaternion.identity) as GameObject;//生成子彈并給它一個向下的力,因為和主角方向反過來的laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projecttileSpeed);AudioSource.PlayClipAtPoint(shootSFX, Camera.main.transform.position, shootSoundVolume);}private void OnTriggerEnter2D(Collider2D other){DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();if (!damageDealer) { return; }ProcessHit(damageDealer); //同樣也是傷害處理的函數(shù)}private void ProcessHit(DamageDealer damageDealer){ health -= damageDealer.GetDamage();damageDealer.Hit();if (health <= 0){Die();}}private void Die(){FindObjectOfType<GameSeesion>().AddToScore(scoreValue);Destroy(gameObject);GameObject explosion = Instantiate(deathDFX,transform.position,transform.rotation);Destroy(explosion, 1f);AudioSource.PlayClipAtPoint(deathSFX, Camera.main.transform.position,deathSoundVolume);} }其次顯示生命值的腳本也要創(chuàng)建好
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScoreDisplay : MonoBehaviour {Text scoreText;GameSeesion gameSession;private void Start(){scoreText = GetComponent<Text>();gameSession = FindObjectOfType<GameSeesion>();}private void Update(){scoreText.text = gameSession.GetScore().ToString();} }?
創(chuàng)建Start和Game Over場景
其實
其實我前面教的差不多了,這里就直接把預(yù)設(shè)體拖進來就好
?
這里的Score Canvas是只需要score即可不需要生命的text
別忘了掛載onclike()監(jiān)聽事件
?
?
設(shè)置連貫的背景音樂:
為了防止音樂在切換場景時重新播放,我們也要創(chuàng)建一個單例給它
先空對象Music Player然后創(chuàng)建同名腳本
using System.Collections; using System.Collections.Generic; using UnityEngine;public class MusicPlayer : MonoBehaviour {private void Awake(){SetUpSingleton();}private void SetUpSingleton(){if(FindObjectsOfType(GetType()).Length > 1){Destroy(gameObject);}else{DontDestroyOnLoad(gameObject);}} }別忘了讓它一直循環(huán)。
?
生成游戲:
別忘了放到build setting上然后點擊PlayerSetting改圖標(biāo)
?
?游戲畫面如下:
?
?
?
總結(jié)
以上是生活随笔為你收集整理的【Unity2D入门教程】简单制作战机弹幕射击游戏⑥最终回扩展其它范围的内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux scp密码配到命令里,Lin
- 下一篇: TV的端口