【Unity2D入门教程】简单制作一个弹珠游戏之制作场景②(设置空气墙,制作UI)

学习目标:

本期就教大伙怎么设置讲究的空气墙以及制作下关卡的UI,内容没什么含金量,我尽量将快点和简单点。


设置空气墙:

创建一个空对象叫PlaySpace并把Background(也就是这个蓝色的背景)拖进来,创建三个空对象左右上Wall,然后添加BoxCollider2D保证边缘与游戏场景的边缘吻合

除此之外我们还要创建一个底下的触发器,当球碰到时就触发切换游戏结束场景的代码

给个新脚本        叫LoseCollider.cs

代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LostCollider : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Ball"))
        {
            SceneManager.LoadScene("GameOverScene");
        }
    }
} //当触发器触发到物体的标签是Ball就加载名字为GameOverScene的场景


之后就是制作UI了

我们只需要制作分数即可

逻辑是分数是会随不同关卡继承下去的,但当游戏重新开始时,分数会清零

GameStatus.cs如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GameStatus : MonoBehaviour
{
    [Range(0.1f,1f)][SerializeField] float gameSpeed = 1f;
    [SerializeField] float currentScore = 0;
    [SerializeField] float PointsPerBlocksDestoryed = 83f;
    [SerializeField] TextMeshProUGUI scoreText;

    [SerializeField] bool isAutoPlayEnabled;
    private void Awake()
    {
        int gameStatusCount = FindObjectsOfType<GameStatus>().Length; //创建实例
        if (gameStatusCount > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject); //不要销毁这个脚本的游戏对象
        }
    }
    void Start()
    {
        scoreText.text = currentScore.ToString();
    }
    void Update()
    {
        Time.timeScale = gameSpeed; //控制游戏速度
    }
    public void AddToScore() //Ball碰到砖块后调用
    {
        currentScore += PointsPerBlocksDestoryed;
        scoreText.text = currentScore.ToString();
    }
    public void ResetGame() //游戏结束按重新开始后
    {
        Destroy(gameObject);
    }
    public bool IsAutoBackEnabled() //是否自动玩游戏
    {
        return isAutoPlayEnabled;
    }
}

可以看到分数在游戏结束后刷新了。

猜你喜欢

转载自blog.csdn.net/dangoxiba/article/details/124029490
今日推荐