【问题标题】:Unity 3D Spinning a gameobjectUnity 3D 旋转游戏对象
【发布时间】:2012-05-16 01:19:26
【问题描述】:

我正在尝试统一旋转 3D 游戏对象。它实际上是一个圆柱体,上面只有一个扑克筹码的纹理。一旦它与光线投射相撞,我想将它旋转 360 度。它在 Unity 仿真器中工作得很好,但是,在设备本身上,芯片在其旋转后停止,然后继续在无限循环中旋转。这是有问题的代码的sn-p。提前感谢您的帮助。

    // Spin the chip
if (Animate) {
    if (Speed > 0 && Chip.tag.Contains("Chip")) {
        Chip.transform.Rotate(0, Speed*Time.deltaTime, 0);
        Speed -= 3;
        Debug.Log(Speed);
    }
    else {
        // Reset
        Animate = false;
        Speed = 360;
        Chip.transform.localRotation = Quaternion.Euler(0.0,0.0,0.0);
    }   
}

为了总结这一点,我可以尽我所能,当它在光线投射上碰撞时分配游戏对象芯片

// Set the chip
    Chip = hit.transform;

一切都在更新函数中完成。一旦光线投射命中,它就会调用一个投注函数,然后在计算投注后,它将布尔动画更改为 true,从而导致芯片旋转。

【问题讨论】:

  • debug.log 告诉你什么?
  • Debug.Log(Speed) 只是向我显示正在倒计时的整数速度。
  • 它是 0 吗? 0点后继续?
  • 是的,它的命中为零,如果不命中也没关系,因为如果它大于 0,所以如果它跳过 0 并命中 -1,它仍然会中断到 else。它在模拟器中运行良好,只是在设备上没有。

标签: animation unity3d unityscript


【解决方案1】:

在其他代码中设置了Animate = true,如果没有看到其余部分,很难判断发生了什么。

在 Animate 设置为 true 的每个位置旁边放置一些调试,您应该会看到其他设置它,只有可能解释它为什么继续旋转。

另一种选择是使用动画工具,而不是旋转,您只需播放为您执行旋转的动画。

编辑:可能是触摸代码,因为当您在编辑器中调试时,您使用击键。我经历过几次的陷阱。

【讨论】:

    【解决方案2】:

    James Gramosli 是正确的,因为其他一些代码再次触发了动画,并且很可能是您的触摸代码。在编辑器和触控设备之间移动时,这是一个常见问题。您可以通过使用 UnityRemote 来验证您的代码的控制流来确定是否是这种情况。

    也就是说,我会将您的代码更改为以下代码,从运行每一帧的更新循环中删除旋转代码。这是一个小的优化,但主要是清理架构并使其更加模块化和整洁。

    从您的代码 sn-p 中不清楚,但我假设您使用的是 UnityScript。

    在您单击芯片时处理触摸代码的脚本中,插入以下行:

    hit.transform.SendMessage("Spin", hit.transform, SendMessageOptions.DontRequireReceiver);
    

    将此代码放入名为“SpinChip”的单独脚本中,然后将该脚本添加到您的芯片对象中。

    var StartSpeed = 360.0;
    var Deceleration = 3.0;
    
    function Spin()
    {
        if (Animating)
        {
            print("Chip is already spinning, not starting another animation");
            return;
        }
    
    /*
        This code isn't necessary if this exists in a separate script and is only ever attached to the clickable chip
        if (!gameObject.tag.Contains("Chip"))
        {
            print("That wasn't a chip you clicked");
            return;
        }
    */
    
        print("Chip has been told to spin");
        StartCoroutine(SpinningAnimation);
    }
    
    function SpinningAnimation()
    {
        print("Chip spin start");
        transform.localRotation = Quaternion.identity;
        Speed = StartSpeed;
        Animating = true;
        while (Speed > 0)
        {
            transform.Rotate(0, Speed*Time.deltaTime, 0);
            Speed -= Deceleration;
            yield; // wait one frame
        }
    
        print("Chip has completed the spin");
        Animating = false;
    }
    

    此代码的作用是创建一个协同例程,在激活时每个更新循环运行一次,它将为您旋转芯片,并且独立于您的实际按钮单击代码。

    【讨论】:

      【解决方案3】:
      var rotSpeed: float = 60; // degrees per second
      
      function Update(){
        transform.Rotate(0, rotSpeed * Time.deltaTime, 0, Space.World);
      }
      

      这是一个旋转游戏对象的代码,你可以将它与矢量 3 一起使用:transform.Rotate(x, y, z); 或空间 transform.Rotate(x, y, z, Space.World); rotSpeed 是旋转速度。

      【讨论】:

      • 您的回答不是对问题的回答。该问题描述了一个不同的问题。
      【解决方案4】:

      在您的更新功能中。 Bool 变量 Animate 可能变为 true 。这可能是您的圆柱体继续旋转的原因。

      其他解决方案是:您可以创建圆柱体的动画,然后使用秒表。这样一段时间后,您就可以使用秒表的时间停止动画

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多