欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

【Unity2D入门教程】简单制作战机弹幕射击游戏⑥最终回扩展其它范围的内容

发布时间:2024/3/26 编程问答 59 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【Unity2D入门教程】简单制作战机弹幕射击游戏⑥最终回扩展其它范围的内容 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

制作分数和生命的UI:

由于我们前面没有做类似的UI所以这里教大伙一下基本思路:

首先我们创建一个canvas用来创建两个Text用来显示分数和生命的UI

蓝色的是分数黄色的是生命

我们创建一个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();} }

创建一个空对象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;//战机在屏幕能移动的坐标float xMin;float xMax;float yMin;float yMax;//协程的编程是指在不堵塞主线程的情况下执行某些特定的函数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()是将摄像机视角的坐标转化为世界坐标然后padding是防止战机出屏幕边缘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上两个监控键盘上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); //暂停某个协程// StopAllCoroutines();}}//协程函数是用关键字迭代器IEnumerator而且一定要用yield关键词返回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); //下一颗子弹发生的间隔时间}}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; //计时器,用来当计时器小于等于0时重置发射时间并执行发射函数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); //同样也是伤害处理的函数}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);} }

其次显示生命值的脚本也要创建好

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();} }

 


创建Start和Game Over场景

其实

其实我前面教的差不多了,这里就直接把预设体拖进来就好

 

这里的Score Canvas是只需要score即可不需要生命的text

别忘了挂载onclike()监听事件


 

 

设置连贯的背景音乐:

为了防止音乐在切换场景时重新播放,我们也要创建一个单例给它

先空对象Music Player然后创建同名脚本

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);}} }

别忘了让它一直循环。


 

生成游戏:

别忘了放到build setting上然后点击PlayerSetting改图标

 

 游戏画面如下:

 

 

 

总结

以上是生活随笔为你收集整理的【Unity2D入门教程】简单制作战机弹幕射击游戏⑥最终回扩展其它范围的内容的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。