c# - Unity 每次点击都会增加 90º

标签 c# unity3d

我想在每次单击按钮时将图像旋转 90º。我的问题是,当我点击它时,它只转向正好 90º。当我再次单击时,它不会转到 180º、270º 并在第四次单击时返回到 0º。

public Image image;

public void WheelClicker()
{
    image.gameObject.transform.DORotate(new Vector3(0, 0, image.gameObject.transform.rotation.z + 90), 1);
}

最佳答案

transform.rotation 是一个 Quaternion值(value)!除非您确切地知道自己在做什么,否则永远不要触摸它的各个组件(xyzw). transform.rotation.z 的值不是您正在寻找的值!

您应该使用 transform.eulerAngles

public Image image;

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Space)) WheelClicker();
}

public void WheelClicker()
{
    // always prevent concurrent animations
    if (DOTween.IsTweening(image.transform)) return;

    image.transform.DORotate(image.transform.eulerAngles + new Vector3(0, 0, 90), 1);
}

enter image description here

关于c# - Unity 每次点击都会增加 90º,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57469572/

相关文章:

c# - 在 C# 中查找字符串中的所有模式索引

c# - 当 Controller 位于另一个项目中时,Get 方法不返回值

c# - LINQ to JSON - 选择个别属性属于导航属性

c# - 为双边移动统一镜像 Oculus Rift Controller 位置

Unity3D - 如何在使用 Shader Graph 制作的着色器中关闭 "receive shadows"?

匿名类型的 C# 类型转换异常,为什么?

c# - 收集金币时不播放声音

c# - Unity3d 和 UdpClient

c# - 如何在 Unity 中从 Android 获取枚举

unity3d - Unity 构建中超过 70% 被文件头占用