c#-Unity3D中的脚本仅debug.log而不是旋转

问题描述 投票:1回答:1

在我的统一3D项目中,我有一个带有子对象的地平面舞台。当我按下UI按钮时,对象应该旋转,如果释放按钮,旋转应该停止。不幸的是,它不起作用。它仅显示我的Debug.Log函数,但对象不会旋转。

这是我的剧本:

using System.Collections;
using System.Collections.Generic;
using Unity.Engine;

public class RotateCube : MonoBehaviour
{
    public Rigidbody rb;
    public rotateStatus = false;
    //public float rotationSpeed = 100f;

    public void rotateNow()
    {
        rotateStatus = !rotateStatus;
        Debug.Log("Rotation position = " + transform.eulerAngles.y)
    }

    void Update()
    {
        if(rotateStatus)
        {
             rb.transform.Rotate(0, 45, 0, Space.World);
             // Also tried it with;
             // rb.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
             Debug.Log("Rotation should been happend.");
        }
     }
}

该脚本已附加到我的gameObject。 UI按钮具有一个带有gameObject的OnClick事件,并链接到rotateNow函数。

我也尝试了Input.GetMouseButtonDown(0)Input.GetButtonDown()方法。可行,但是每次我按下屏幕时,使用这些方法,我的对象就会旋转。

这是我在iPad上进行测试时控制台显示的内容:

((文件名:./Runtime/Export/Debug.bindings.h第45行)

应该发生旋转。

希望有人可以帮助我解决这个问题

编辑:

enter image description here

c# unity3d
1个回答
0
投票

这会将对象旋转约45°每帧 ...只是为了让您的眼睛快一点

您应该使用Time.deltaTime以便将旋转转换为平滑的“旋转/秒”

Time.deltaTime

然后,当涉及到Rotate(0, 45 * Time.deltaTime, 0, Space.World); 时,您应该not通过Rigidbody组件设置任何转换,而应使用Transform中的RigidBody.MoveRotation来旋转对象但保持完整的物理

RigidBody.MoveRotation

如果释放按钮,则旋转应停止

这不会发生。 Unity的默认按钮没有用于告诉脚本不再按下该按钮的实现。

您可以使用FixedUpdatevoid FixedUpdate() { if(rotateStatus) { rb.MoveRotation(rb.rotation * Quaternion.Euler(Vector3.up * rotationSpeed * Time.deltaTime)); Debug.Log("Rotation should be happening."); } } 接口为此编写自己的扩展名:

IPointerUpHandler

将此按钮附加到您的按钮上,并在IPointerUpHandler中也引用您的IPointerExitHandler,以便在释放按钮时将重置IPointerExitHandler

© www.soinside.com 2019 - 2024. All rights reserved.