【问题标题】:Stop Z rotation of gameObject on touch in Unity在 Unity 中触摸时停止游戏对象的 Z 旋转
【发布时间】:2017-07-05 12:54:19
【问题描述】:

嗨,我正在尝试在触摸时在 X 轴和 Y 轴上旋转游戏对象,代码运行良好,但它也会影响 z 旋转,我不想要 Z 旋转, 我的游戏对象不包含刚体,所以我不能应用 Z 旋转约束。我已经在互联网上搜索了这个问题,但找不到任何合适的解决方案。 任何帮助,将不胜感激。谢谢x

这是我的代码:

public class Rot : MonoBehaviour {
    private float RotateSpeed   = 10f;

    void OnMouseDrag () {
        float xRot = Input.GetAxis ("Mouse X") * RotateSpeed * Mathf.Deg2Rad;
        float yRot = Input.GetAxis ("Mouse Y") * RotateSpeed * Mathf.Deg2Rad;

        transform.RotateAround (Vector3.up, -xRot);
        transform.RotateAround (Vector3.right, yRot);    
    }
}

【问题讨论】:

  • 我认为您应该使用另一种旋转算法或在官方统一文档中提醒transform.RotateAround 是如何工作的。 transform.RotateAround 这就是围绕 Z 轴旋转游戏对象的原因。
  • @KamikyIT 你能指导我换一种方式而不是 transform.RotateAround
  • 为什么不改用 transform.Rotate() 或 transform.localEulerAngles?
  • @SurajS 我知道transform.Rotate() 和transform.localEulerAngles 的语法和用法,但是你能告诉我如何在我的情况下使用它

标签: c# android unity3d rotation


【解决方案1】:

试试这个,

transform.Rotate(new Vector3(-xRot,yRot,0.0f),Space.Self);

如果你想设置绝对旋转使用transform.localEulerAngles

【讨论】:

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