【问题标题】:If we pass float value to Vector3Int, how does it behave?如果我们将浮点值传递给 Vector3Int,它的行为如何?
【发布时间】:2019-07-02 21:11:30
【问题描述】:

如果我们将一些浮点值传递给 Vector3Int 的参数,它是如何处理的?

m_target = new Vector3Int(target.x-speedx/4,target.y,target.z-speedz/4);

所以如果 speedx/4 将是浮点数,那么它将如何转换为 int

Vector3Int(124.66,0,0) 将被视为 (124,0,0) 或 (125,0,0)

【问题讨论】:

  • target.xspeedx是什么类型?
  • @LasseVågsætherKarlsen 他们都是 int
  • 那么你根本没有浮点值,int/int 给出另一个int,例如如果speedx = 15speed / 4 将给出3,而不是3.33333.....
  • @LasseVågsætherKarlsen 所以基本上自动类型转换是在 speedx/4 上完成的,而不是 (target.x - speedx/4)?
  • 不,没有类型转换,int 之间的/ 产生另一个 int,不涉及转换。这只是整数除法。基本上你有int + int / int,所有这些只会产生另一个int,float从来没有出现在图片中。

标签: c# unity3d


【解决方案1】:

它不会编译。

您有多种选择将值转换为int取决于您的需要

但首先要特别确保 speedx 的类型为 float 或使用 4f 否则您可能已经使用不正确的结果在那里进行了 int 除法


简单类型转换 | FloorToInt

new Vector3Int((int)(target.x - speedx/4f), (int)(target.y), (int)(target.z-speedz/4f));

它只是“删减”逗号后面的任何内容。因此它的行为类似于Mathf.FloorToInt

(124.6f, 0, 0) → (124, 0, 0)
(124.4f, 0, 0) → (124, 0, 0)


RoundToInt

您可以改用Mathf.RoundToInt

new Vector3Int(Mathf.RoundToInt(target.x-speedx/4f), Mathf.RoundToInt(target.y), Mathf.RoundToInt(target.z-speedz/4f)); 

正确地将值向上和向下舍入为有效的int

(124.6f, 0, 0) → (125, 0, 0)
(124.4f, 0, 0) → (124, 0, 0)


CeilToInt

如果你想要它总是被搁置,你也可以使用Mathf.CeilToInt

new Vector3Int(Mathf.CeilToInt(target.x-speedx/4f), Mathf.CeilToInt(target.y), Mathf.CeilToInt(target.z-speedz/4f)); 

(124.6f, 0, 0) → (125, 0, 0)
(124.4f, 0, 0) → (125, 0, 0)

【讨论】:

    【解决方案2】:

    在对问题的评论后添加的注释

    在您发布的代码中,您正在执行整数除法,发送结果为 int(另请参阅 this answer)。

    根据我在 cmets 中读到的内容。 target.xtarget.yspeedxspeedz(以及常数 4)都是整数。

    另见此示例:

    int x = 13 / 4; //this returns int 3
    

    所以你不需要在那里投射。


    问题的答案浮动到 Vector3Int

    Float 不会隐式转换为 int,因此您的代码不会编译。您需要进行转换。

    还要考虑转换会截断浮点数。

    float x = 124.345f;  
    int y = (int) x;   //x will become 124.
    

    在您的具体情况下,您可以像这样转换它。

    m_target = new Vector3Int((int) target.x-speedx/4, (int)target.y, (int)target.z-speedz/4);
    

    结果将是 (124.0, 0.0, 0.0)。

    如果您想获得其他结果,可以检查这些方法。


    浮点到整数的转换

    Mathf.FloorMath.FloorToInt

    最终结果将与基础演员一样。浮点数将被截断为较低的 int。

    float x 124.345;
    int y = (int) Mathf.Floor(x); //y will become 124
    //or
    int z = Mathf.FloorToInt(x);  //z will become 124
    

    转化结果
    浮动124.345f →转换为 int 124
    浮动124.789f →转换为 int 124


    Mathf.CeilMathf.CeilToInt

    浮点数将被截断为更高的整数。

    float x 124.345;
    int y = (int) Mathf.Ceil(x); //y will become 125
    //or
    int z = Mathf.CeilToInt(x); //z will become 125
    

    转化结果
    浮动124.345f →转换为 int 125
    浮动124.789f →转换为 int 125


    Mathf.RoundMathf.RoundToInt

    浮点数将四舍五入到更接近的整数。

    float x 124.345;
    int y = (int) Mathf.Round(x); //y will become 124
    //or
    int z = Mathf.RoundToInt(x);  //z will become 124
    

    转换结果不同
    浮动124.345f →转换为 int 124
    浮动124.789f →转换为 int 125

    【讨论】:

    • 您的所有链接仍然返回float ;) 看看my answer,您必须使用int 版本。
    • 是的,抱歉,我已经在编辑答案了。感谢您的提醒。
    • 请注意,它仍然不正确:Math 没有像 RoundToInt 这样的方法 .. 它是 Unity 的库 Mathf(注意 f
    • 我知道我们需要将它类型转换为 int,但我的问题是我在 Erlang 中转换了一些 c# 代码,所以 c# 代码我无法更改,我想知道我应该怎么做在 erlang 中(考虑到 erlnag 没有此 Vector3Int 的 ceil 或 floor 或 roundof),那么如果在 c# 中我们将浮点值作为参数,我应该如何处理
    • derHugo,没错。 @PrateekPandey。你到底需要传递什么给 Erlang?您可能只是在 C# 中进行转换并将结果传递给 Erlang。
    猜你喜欢
    • 2018-04-19
    • 2022-12-23
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多