c# - 如何从分数中取整数?
问题描述
我想从我的分数中取出整数并将其添加到我的“硬币”示例:这个想法是当玩家到达游戏结束时或当他死亡时,他的分数被交换为硬币。想法是如果我放 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);
}
解决方案
您可以使用Mathf.FloorToInt()来完成此操作
int score = 245;
int coins = Mathf.FloorToInt(score / 50);//will give 4
推荐阅读
- python - 在 Heroku 上安装 Azure pyodbc 驱动程序
- javascript - 如何通过两个组件传递我的函数?
- typescript - 使用 TS 编译后,导入类在文件之间不起作用
- wpf - 在视觉状态下更改标签前景色
- dns - 无法使用 CNAME 解析主机但可以直接解析?
- tensorflow - Tensorflow-MKL 在 3D 数据上给出错误
- authentication - Application validation with whatsapp
- r - R:如何使用 Quanteda 获取文件名:char_segment
- php - php + html 基本格式
- android - 找不到与给定名称 @integer/google_play_services_version 匹配的资源