小编The*_*tor的帖子

unity c# Mathf.FloorToInt(-0.5) 返回 0 (作用类似于 Mathf.CielToInt)

这是Mathf.FloorToInt的脚本参考文档正如您所看到的,它应该将 -0.5 舍入到 -1。由于某种原因,当与我的计算一起使用时,它似乎将其返​​回为 0。

我有相同函数的两个版本,它们的工作方式非常相似,但输出不同。我的代码只会向这些函数提交 3 到 18 之间的整数。

此版本的行为就像使用 Mathf.CielToInt (在 statRoll = 9 的情况下返回 0):

    public int getBonus(int statRoll)
{
    int result = Mathf.FloorToInt((statRoll - 10) / 2);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

这是有效的版本(在 statRoll = 9 的情况下返回 -1):

    public int getBonus(int statRoll)
{
    float initial = statRoll - 10;
    float divided = initial / 2;
    int result = Mathf.FloorToInt(divided);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

c# floor unity-game-engine math-functions

2
推荐指数
1
解决办法
2986
查看次数

标签 统计

c# ×1

floor ×1

math-functions ×1

unity-game-engine ×1