团结 - 我如何获得高分?

时间:2017-10-13 12:34:31

标签: c# unity3d

我正在制作一个生存射击游戏,而我正试图这样做,当我杀死敌人时,它会为我的分数增加分数。分数被添加到高分并且在每次比赛后保存。我有添加分数的脚本,但我不认为我的分数很高。有人可以帮我这个吗?

public class ScoreManager : MonoBehaviour
{
    public static int score;
    public static int highScore;

    Text scoreText;
    Text highScoreText;

    void Awake ()
    {
        scoreText = GetComponent <Text> ();
        highScoreText = GetComponent<Text>();
        score = 0;
        highScore = 0;

        highScoreText.text = PlayerPrefs.GetInt("High Score", 0).ToString();
    }
    void Update ()
    {
        scoreText.text = "Score: " + score;
        highScoreText.text = "High Score: " + highScore;
        if(score>PlayerPrefs.GetInt("High Score", 0))
        {
            PlayerPrefs.SetInt("High Score", score);
            highScoreText.text = score.ToString();
        }
    }
}  

这是在敌人死亡时加入得分的敌人健康脚本。 它在敌人的沉没功能中。

public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;
    public int currentHealth;
    public float sinkSpeed = 2.5f;
    public int scoreValue = 10;
    public AudioClip deathClip;


    Animator anim;
    AudioSource enemyAudio;
    ParticleSystem hitParticles;
    CapsuleCollider capsuleCollider;
    bool isDead;
    bool isSinking;


    void Awake ()
    {
        anim = GetComponent <Animator> ();
        enemyAudio = GetComponent <AudioSource> ();
        hitParticles = GetComponentInChildren <ParticleSystem> ();
        capsuleCollider = GetComponent <CapsuleCollider> ();

        currentHealth = startingHealth;
    }


    void Update ()
    {
        if(isSinking)
        {
            transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
        }
    }


    public void TakeDamage (int amount, Vector3 hitPoint)
    {
        if(isDead)
            return;

        enemyAudio.Play ();

        currentHealth -= amount;

        hitParticles.transform.position = hitPoint;
        hitParticles.Play();

        if(currentHealth <= 0)
        {
            Death ();
        }
    }


    void Death ()
    {
        isDead = true;

        capsuleCollider.isTrigger = true;

        anim.SetTrigger ("Dead");

        enemyAudio.clip = deathClip;
        enemyAudio.Play ();
    }


    public void StartSinking ()
    {
        GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
        GetComponent <Rigidbody> ().isKinematic = true;
        isSinking = true;
        ScoreManager.score += scoreValue;
        Destroy (gameObject, 2f);
    }
}  

但我面临两个问题:

  1. 当我重新开始游戏时,高分文本会替换分数文本
  2. 当我杀死敌人时,得分文本不会更新

3 个答案:

答案 0 :(得分:1)

根据您的评论:

  

当我停止比赛并再次比赛时,得分就高了   屏幕。但是当我玩的时候,高分文本取代了分数文本,   当我杀死一个敌人时它并没有计算在内

当你杀死敌人时你的分数没有增加的原因是因为当你的敌人死亡时你没有打电话给StartSinking ()。正如我在该方法中看到的那样,您可以更改ScoreManager.score

的值
public void StartSinking ()
{
    GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
    GetComponent <Rigidbody> ().isKinematic = true;
    isSinking = true;
    ScoreManager.score += scoreValue;
    Destroy (gameObject, 2f);
}

因此,您应该在Death()调用StartSinking ()

void Death ()
{
    isDead = true;

    capsuleCollider.isTrigger = true;

    anim.SetTrigger ("Dead");

    enemyAudio.clip = deathClip;
    enemyAudio.Play ();
    StartSinking();
}

答案 1 :(得分:0)

没有必要每帧都更新高分,你应该只在游戏结束时保存新的高分。您可以在游戏开始时获得高分,然后在游戏中使用该值,当游戏结束时您更新高分的价值

您也从未为highScore分配任何值,它始终为0.将文本组件公开并将其添加到检查器中也会更容易。

public class ScoreManager : MonoBehaviour
{
public static int score;
public static int highScore;

public Text scoreText;
public Text highScoreText;

void Awake()
{
    score = 0;
    highScore = PlayerPrefs.GetInt("High Score", 0);

    highScoreText.text = "High Score: " + highScore;
}
void Update()
{
    scoreText.text = "Score: " + score;
    highScoreText.text = "High Score: " + highScore;
    if (score > highScore)
    {
        highScoreText.text = "High Score: " + score;
    }
}
}

然后,无论你在哪里结束游戏,都要添加保存高分的代码。

答案 2 :(得分:0)

  

当我停止游戏并再次玩游戏时,屏幕上的分数很高。但是当我演奏的时候,高分文本取代了乐谱文本,当我杀死一个敌人时它并不算数

您的代码中有两个GetComponent<Text>()。他们似乎正在引用相同的组件,导致您的高分覆盖您的分数文本。尝试创建空的gameobjects来保存每个文本元素,然后使用public Text scoreTextpublic Text highScoreText并将两个游戏对象拖到Unity编辑器中此脚本中的适当位置。

E.g。 HeirarchyComponent

此外,您实际上并没有更新您的高分。由于您的文本组件依赖于您的基础分数变量,因此您无法从文本开始并保留高分0.您需要确保更新变量然后根据它们更新文本,否则您可以开始你的文字得分高,但随后你的高分没有被更新,它会立即在下一个Update()被重写为0。

以下内容应该是您所需要的:

Awake()中的

// get high score from PlayerPrefs
highScore = PlayerPrefs.GetInt("High Score", 0);
Update()中的

void Update ()
{
    scoreText.text = "Score: " + score;
    if (score > highScore)
    {
        highScore = score;
        // only update high score text when it actually changes
        highScoreText.text = "High Score: " + highScore;
    }
}

游戏结束后:

int originalHighScore = PlayerPrefs.GetInt("High Score", 0);
// compare current session's high score vs our recorded high score from prefs
if (highScore > originalHighScore)
{
    PlayerPrefs.SetInt("High Score", highScore);
}
相关问题