如何从分数中取出整数?
How to take whole numbers from score?
我想从我的分数中取出整数来添加到我的 "Coins"
例子:
这个想法是当玩家到达游戏结束时或者当他死亡时,他的分数会被换成硬币。
想法是如果我把
40 = 1 个硬币。
如果他得分 85,我们就给他 2 个硬币。如果他得分 145,我们将给他 3 个硬币。
或者如果我定义
50 分 = 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);
}
您可以使用 Mathf.FloorToInt() 来完成此操作
int score = 245;
int coins = Mathf.FloorToInt(score / 50);//will give 4
我想从我的分数中取出整数来添加到我的 "Coins" 例子: 这个想法是当玩家到达游戏结束时或者当他死亡时,他的分数会被换成硬币。 想法是如果我把 40 = 1 个硬币。 如果他得分 85,我们就给他 2 个硬币。如果他得分 145,我们将给他 3 个硬币。 或者如果我定义 50 分 = 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);
}
您可以使用 Mathf.FloorToInt() 来完成此操作
int score = 245;
int coins = Mathf.FloorToInt(score / 50);//will give 4