首页 > 解决方案 > Ground Plane Stage 旋转子对象

问题描述

在我的项目中,我有一个带有子对象的地平面舞台。现在我想用 UI Button 旋转这个对象。如果我按住按钮,它应该旋转,如果我松开按钮,旋转应该停止。不幸的是,我无法做到这一点。

这是我的脚本:

public class RotateObject : MonoBehaviour
{
    public float rotationSpeed = 45f;
    public bool isPressed = false;

    public void TogglePressed(bool value)
    {
        isPressed = !isPressed;
    } //edit added missing Brace

    void Update()
    {
        if (isPressed)
        {
            transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
        }
    }
}

我添加Event TriggerPointer DownPointer Up按钮。该脚本附加到我的游戏对象,并且该TogglePressed函数Pointer Down与选中的复选框链接到Pointer Up,此处未选中复选框。

如果我在 Unity 中测试它并单击按钮,检查器显示它可以工作,但如果我将它上传到我的手机,则对象没有旋转。

编辑:

Input.GetMouseButtonDown()

void Update()
{
    if (isPressed && Input.GetMouseButtonDown(0))
    {
        // Same Code
    }

}

Input.GetButtonDown()

void Update()
{
    if(isPressed && Input.GetButtonDown("Fire1"))
    {
        //Same Code
    }
}

编辑2:

这是我现在使用的脚本:

public class RoatateObject : MonoBehaviour
{
    public float rotationSpeed = 45f;

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
           transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
        }
    }
}

标签: c#unity3d

解决方案


我不确切知道是否可以在手机上识别pointerdown,您是否尝试过 Input.GetMouseButtonDown() (也在手机上触发)?

也许为了加快你的测试尝试统一远程测试本地而不是上传到你的手机


推荐阅读