首页 > 解决方案 > 如何从分数中取整数?

问题描述

我想从我的分数中取出整数并将其添加到我的“硬币”示例:这个想法是当玩家到达游戏结束时或当他死亡时,他的分数被交换为硬币。想法是如果我放 40 = 1 个硬币。如果他得分 85 ,我们给他 2 个硬币。如果他得分 145,我们给他 3 个硬币.. 或者如果我定义 50scores = 1 个硬币。如果他得到 245 分,他会得到 4 个金币。

我试图用我的知识做同样的事情,但他得到的硬币有时会增加太多,玩家得到 4 个而不是 2 个硬币。

这是我的代码,用于查找分数并尝试计算给玩家多少,但正如我所说,它不起作用。

int points = (GameManager.Instance.score / 40);

Debug.Log("POINTS ARE" + points);

if (points >= 1)
{
    gold = points;

    int goldl = PlayerPrefs.GetInt("gold");
    goldl += gold;

    //Debug.Log("here is finished gold" + goldl);

    PlayerPrefs.SetInt("gold", goldl);
}

标签: c#unity3dgame-development

解决方案


您可以使用Mathf.FloorToInt()来完成此操作

int score = 245;
int coins = Mathf.FloorToInt(score / 50);//will give 4

推荐阅读