停止计分在玩家被摧毁后?

huangapple go评论72阅读模式
英文:

Stopping score count after player is destroyed?

问题

我试图制作我的第一个游戏,但我一直被这个问题困扰了一段时间。我似乎无法弄清楚如何检查玩家是否死亡并停止记分。我也想知道如何存储分数。

我的游戏很简单。你必须尽量躲避很多球,并获得高分。

但是当我的玩家与其中一个球发生碰撞时,游戏仍在继续。我有一个移动前进的脚本用于敌人球,它们以一定的速度移动,还有一个当球经过玩家时销毁并向分数添加5分的“出界销毁”脚本。我有一个“游戏管理器”脚本,负责生成所有的球并显示当前分数。(我可能需要重新编写一些代码对吗?)

以下是我的游戏管理器脚本中的内容。我真的需要知道如何检查玩家是否死亡以及发生情况时如何处理。

using TMPro;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public GameObject[] EnemyPrefabs;
    private float spawnRangeX = 17;
    private float spawnPosZ = 35;
    private float StartDelay = 2.2f;
    private float SpawnInterval = 0.1f;
    public bool PlayerAlive;
    public TextMeshProUGUI scoreText;
    private int score;
    public TextMeshProUGUI gameOverText;

    void Start()
    {
        score = 0;
        UpdateScore(0);

        InvokeRepeating("SpawnRandomEnemy", StartDelay, SpawnInterval);
    }

    void Update()
    {
        PlayerAlive = true;
    }

    public void UpdateScore(int scoreToAdd)
    {
        score += scoreToAdd;
        scoreText.text = "Score: " + score;
    }

    void SpawnRandomEnemy()
    {
        int enemyIndex = Random.Range(0, EnemyPrefabs.Length);
        Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
        Instantiate(EnemyPrefabs[enemyIndex], spawnPos, EnemyPrefabs[enemyIndex].transform.rotation);
    }
}

在“玩家”脚本中,我有一个“private void OnTriggerEnter(Collider other)”以使玩家被销毁并激活“游戏结束”文本。我还在那里放了一个“PlayerAlive = false”,因为我还在努力弄清楚这一点。

英文:

I'm trying to make my first game but I've been stuck on this problem for a while now. I can't seem to figure out how to check if the player is dead and stop score count. Also I want to know how to store that score.

My game is simple. You have to dodge a lot of spheres for as long as you can and get the high score.

But when my player collides with one of the spheres, the game still carries on. I have a "Move Forward" script for the enemy spheres that move at a certain speed, and a "Destroy Out of Bounds" script for when the spheres pass the player, they get destroyed off-screen and add 5 points to the score. I do have the "Game Manager" script that spawns all the spheres and to show the current score. (I may need to re-route some coding right?)

Here's what's in my Game Manager script. I need to know how to really check if the player is dead when something happens.

using TMPro;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public GameObject[] EnemyPrefabs;
    private float spawnRangeX = 17;
    private float spawnPosZ = 35;
    private float StartDelay = 2.2f;
    private float SpawnInterval = 0.1f;
    public bool PlayerAlive;
    public TextMeshProUGUI scoreText;
    private int score;
    public TextMeshProUGUI gameOverText;
    // Start is called before the first frame update
    void Start()
    {
        score = 0;
        UpdateScore(0);

        InvokeRepeating("SpawnRandomEnemy", StartDelay, SpawnInterval);

    }

    // Update is called once per frame
    void Update()
    {
        PlayerAlive = true;
    }

    public void UpdateScore(int scoreToAdd)
    {
        score += scoreToAdd;
        scoreText.text = "Score: " + score;
    }


    void SpawnRandomEnemy()
    {
        int enemyIndex = Random.Range(0, EnemyPrefabs.Length);
        Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
        Instantiate(EnemyPrefabs[enemyIndex], spawnPos, EnemyPrefabs[enemyIndex].transform.rotation);
    }
}




In the "player" script, I have a "private void OnTriggerEnter(Collider other)" to have the player be destroyed and activate the "game over" text. I also put a "PlayerAlive = false" there because I'm still trying to figure this out.

答案1

得分: 1

以下是翻译好的部分:

问题出在你的代码的这部分:

// Update is called once per frame
void Update()
{
    PlayerAlive = true;
}

这对我来说没有意义,因为只要存在一个GameManager实例,每一帧,PlayerAlive的值都会被设置为true,无论你在其他地方是否将其设置为false。

你应该将这行代码移到你的Start函数中,以初始化PlayerAlive为true。

现在,由于Player类中的特定触发碰撞器会将其设置为false,所以你需要在UpdateScore中添加一个简单的检查,如下所示:

public void UpdateScore(int scoreToAdd)
{
    // 如果玩家已经死亡,则什么都不做
    if (!PlayerAlive) return;

    score += scoreToAdd;
    scoreText.text = "分数: " + score;
}

希望这能解决你的问题!

英文:

The problem is with this part of your code:

// Update is called once per frame
void Update()
{
    PlayerAlive = true;
}

This doesn't make sense to me, because as long as a GameManager instance exists, every frame, the value of PlayerAlive will be updated to true, regardless of if you set it to false somewhere else.

You should move that line of code inside your Start function to initialize PlayerAlive to true.

Now since a specific trigger collider in the Player class will set it to false, what you need to do is add a simple check to UpdateScore like so:

public void UpdateScore(int scoreToAdd)
{
    // if player is dead, then do nothing
    if(!PlayerAlive) return;

    score += scoreToAdd;
    scoreText.text = "Score: " + score;
}

Hope this solves your issue!

huangapple
  • 本文由 发表于 2023年6月9日 00:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定