【问题标题】:Unity3D Rotation not behaving as expectedUnity3D 旋转未按预期运行
【发布时间】:2017-08-23 15:40:22
【问题描述】:

我正在尝试为模拟制作一个小型演示,我认为这部分会很容易。男孩是我错了,现在我完全被困住了。这是我的问题。

我正在尝试使用旋转,对于这个问题,我们将使用 30 度角,如下所示:

绿光是-15度,红光是0度,蓝光是+15度,总共30度。

现在我想将所说的 30 度分成相等的部分,所以第一个好的数字是 10。这意味着我可以每 3 度绘制一条射线,直到达到最大值,在这种情况下为 +15 度。这可以在下面看到:

看起来不错!有少量空间关闭,如果有人知道这是为什么,请告诉我。

现在,我需要大量光线,因此我们将增加 10 倍并将分割数增加到 100,如下所示。另外我需要使用imgur,因为这里只允许使用两张图片,所以请多多包涵,谢谢。

https://imgur.com/a/cWDdj

第一张图片有 100 个块,你可以看到光线都投射到左边。这是个问题。

第二张图片是分割数增加到 1000。现在更糟糕的是,除了 15 度的区域外,它一直围绕着对象。

这是执行此操作的 sn-p。

 if(testRotation)   
 {
      //Sets the objects rotation to the starting rotation which is -15 degeres
      this.transform.Rotate(this.transform.rotation.x, this.transform.rotation.y - 15.0f,           
           this.transform.rotation.z);

      for (int i = 0; i <= 10; i++)
      {
           //Draws a ray at the current position and forwards relative to the objects transform
           Debug.DrawRay(this.transform.position, 
           this.transform.forward * maxSightDistance, Color.cyan, 1000.0f);

           //This advances the rotation by a factor of one chunk 
           //which in this case is 10 which seems to work 
           this.transform.Rotate(new Vector3(this.transform.rotation.x, 
           this.transform.rotation.y + (30/10), this.transform.rotation.z));
      }

      //This makes it so it doesn't run forever
      testRotation = false;

 }

唯一改变的地方是在检查条件的 for 循环中,以及 Rotate 函数的第二个参数,它通过“一个块”添加到当前旋转值。

对于 100 个块,我将其修改为:for(int i = 0; i &lt; 100; i++)this.transform.rotation.y + (30/100)

对于 1000 个块,我将其修改为:for(int i = 0; i &lt; 1000; i++)this.transform.rotation.y + (30/1000)

我完全被卡住了,不知道为什么它正在做它正在做的事情。另外,如果您需要解释或澄清某些事情,请告诉我!

【问题讨论】:

    标签: c# unity3d rotation


    【解决方案1】:

    你将一个数字除以一个整数,这将返回一个整数。相反,您需要除以浮点数以返回十进制数。

    this.transform.rotation.y + (30/100f)
    this.transform.rotation.y + (30/1000f)
    

    编辑:另外,rotate 的分量应该是 0 而不是之前的值,以避免旋转累积。

    transform.Rotate(new Vector3(0, 30/10f, 0));
    

    【讨论】:

    • 所以这解决了 100 段的问题,但除以 1000 仍然会环绕环
    • 我完全理解你的意思,我补充说,对于这两种情况,但即使是浮动形式的 1000 段仍然会在物体周围出现光线环,而在它们应该出现的地方缺少光线.
    • 如果您输入较小的数字(例如 200 或 500)会发生什么情况?
    • 当 200 被放入时,光线变得更紧,但它们在 0 度线之前停止。当放入 500 时,它开始像 1000 一样移动,但没有那么远。据我所知,小部门的轮换开始变成负数?
    • 如果你在 this.transform.Rotate(new Vector3() 的 for 循环中为 x 和 z 分量输入 0,它的作用会有所不同吗?
    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    相关资源
    最近更新 更多