【问题标题】:Unity Highscore reset at respawnUnity 高分在重生时重置
【发布时间】:2018-03-26 14:14:36
【问题描述】:

我正试图在我走进我的“黄金”对象后立即重生并重置分数计数。 至于现在我什至无法重生,这在尝试实施“Score-Stuff”之前是可能的(起初“FoundGold”脚本仅用于能够重生)。我也试图让最低分数成为高分。 请注意,我是 C# 的新手,我将我需要的教程中的所有内容都放在一起,因此非常感谢您提供一些实际代码/说明出现问题的答案。

//GoldFound Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class GoldFound : MonoBehaviour
{
private ScoreManager theScoreManager;
public Transform target;

[SerializeField] private Transform player;
[SerializeField] private Transform respawnpoint;

private void Start()
{
    theScoreManager = FindObjectOfType<ScoreManager>();
}


private void OnTriggerEnter(Collider other)
{
    theScoreManager.scoreIncreasing = false;
    player.transform.position = respawnpoint.transform.position;
    theScoreManager.scoreCount = 0;
    theScoreManager.scoreIncreasing = true;

}


}

其他代码

//ScoreManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class ScoreManager : MonoBehaviour
{

public Text scoreText;
public Text hiScoreText;

public float scoreCount;
public float hiScoreCount;

public float pointPerSecond;

public bool scoreIncreasing; 


// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (scoreIncreasing)
    {
        scoreCount += pointPerSecond * Time.deltaTime;
    }

    if(scoreCount > hiScoreCount)
    {
        hiScoreCount = scoreCount;
    }


    scoreText.text = "Score: " + Mathf.Round (scoreCount);
    hiScoreText.text = "High Score: " + Mathf.Round (hiScoreCount);

}
}

【问题讨论】:

  • 我觉得你应该做的第一件事就是分开你的任务。制定得分重置方法,一些用于重置球员位置等……然后制定适当使用它们的行为,尝试做所有硬编码的事情,这会导致一大碗意大利面条代码。通过拆分它们,您首先保证它们可以工作,然后您可以按照自己的意愿使用它们,而无需修改您的初始代码。
  • 另外,将每次更新时的文本更改为相同的值确实是多余的,您可能正在修改 UI 元素,我问,OnGUI() 在哪里?其次,你应该只在分数发生变化时才刷新分数,而不是每一帧。
  • 第三,我不明白你为什么在一次调用中将theScoreManager.scoreIncreasing设置为truefalse,因为它从未在两者之间使用过,它基本上总是true这让我无法理解它的目的。
  • 如果你想让最低分成为你最好的,只需反转你的 if 语句的条件:if(scoreCount &lt; hiScoreCount) { hiScoreCount = scoreCount; }
  • 好的,如果你能提供一些反馈那就太好了,但我会试着了解你想要做什么,你似乎想做一种跑步某种游戏,你走得越快,你的分数越低,它就越好。也许它正在完成一个迷宫或什么的。从我所看到的情况来看,您如何使用bool theScoreManager.scoreIncreasing 进行操作是无用的。这绝对是真的,至少在接触过 GoldFound 的东西之后是这样。我也没有真正看到对它的系统的需求。没有你也可以这样做。

标签: c# unity3d


【解决方案1】:

如果您想在游戏会话之间保存您的最高分,那么最简单的方法是将值保存到 PlayerPrefs。如果你想开始保存更多/更复杂的东西,你真的应该把它保存在你自己生成的文件中。但在你的情况下,PlayerPrefs 很好。

以下是有关该主题的 Unity 教程: https://unity3d.com/learn/tutorials/topics/scripting/high-score-playerprefs

否则,你可以这样做:

public void SetHighscore (float currentScore)
{
    if (PlayerPrefs.HasKey("highscore"))
    {
        float highscore = PlayerPrefs.GetFloat("highscore");
        if (highscore > currentScore)
        {
            PlayerPrefs.SetFloat("highscore", currentScore);
            PlayerPrefs.Save();
        }
    }
    else
    {
        PlayerPrefs.SetFloat("highscore", currentScore);
        PlayerPrefs.Save();
    }
}

然后只需在需要时编写 PlayerPrefs.GetKey("highscore") 即可。 (虽然我也建议您使用 PlayerPrefs.HasKey("highscore") 检查它是否存在) https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

【讨论】:

  • 哦,在你写 player.transform.position 和 respawnPoint.target.position 的地方,你可以去掉中间的额外“变换”。您已经提到了 Transform 组件,因此您只需编写 player.position。除此之外,您的代码看起来应该可以正常工作。 (除了在cmets中提出的观点)
  • @Jebr 如果此评论回答了您的问题,请接受。如果没有,请提供更多信息,说明哪些仍然不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 2022-06-23
相关资源
最近更新 更多