【问题标题】:Rotate GameObject on Z axis only C#仅在 Z 轴上旋转游戏对象 C#
【发布时间】:2015-05-06 10:28:11
【问题描述】:

在这里找到这个答案: Rotate a gameobject in Unity

我发现这非常有用,但我只想将旋转分配给 Z 轴,我无法开始工作。

using UnityEngine;
using System.Collections;

public class Rotation : MonoBehaviour {
    public GameObject Player;
    public GameObject PlayerCube;

    // Use this for initialization
    void Start () {
        var playerMapPos = Player;
        var playerWorldPos = PlayerCube;
        PlayerCube.transform.rotation  = Player.transform.rotation;
    }

    // Update is called once per frame
    void Update () {
    }
}

【问题讨论】:

  • 发布您尝试的轮换代码
  • 请在使用前阅读标签说明。 unity 标签与 Unity 游戏引擎无关。

标签: c# unity3d rotation gameobject


【解决方案1】:

要限制旋转轴,您需要使用欧拉角而不是四元数。首先,将旋转四元数转换为欧拉角,然后根据需要修改旋转(将除y之外的所有轴分配为0),然后将其转换回来。

【讨论】:

    【解决方案2】:

    您也可以使用四元数,但需要具体说明您要旋转多少以及要应用的 Vector3 方向(即:Vector3.up)

    PlayerCube.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
    

    【讨论】:

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