【问题标题】:Restrict rotation of 3d model in Unity 3d在 Unity 3d 中限制 3d 模型的旋转
【发布时间】:2016-02-12 08:10:16
【问题描述】:

我想根据玩家在 x 和 y 方向上的触摸来旋转我的 3d 模型。 这样我就可以检测到水平和垂直触摸。

但在此我想限制 z 方向的旋转。为此,我尝试了多个代码并在其他论坛中提出建议。目前没有任何建议对我有用。

基本上,我不希望 z 方向旋转。下图给你更多的想法,哪里出了问题。模型完全旋转到 z 方向。我想阻止它。

这是我为实现同一目标而进行的多次尝试。

void Update ()
{
    // If there are two touches on the device...
    if (Input.touchCount == 1 && GameManager.Instance.IsGameStart) {

        // Store currnet touch.
        Touch touch = Input.GetTouch (0);

        //   transform.RotateAround (transform.position, Vector3.up, -     touch.deltaPosition.x  Time.deltaTime  15f);
        //   transform.RotateAround (transform.position, Vector3.right, touch.deltaPosition.y  Time.deltaTime  15f);
        //   transform.RotateAround (transform.position, Vector3.forward, 0f);

        //   transform.Rotate (Vector3.up, -touch.deltaPosition.x  Time.deltaTime  10f, Space.World);
        //   transform.Rotate (Vector3.right, touch.deltaPosition.y  Time.deltaTime  5f, Space.World);
        //   transform.Rotate (Vector3.forward, 0f, Space.World);

        myRigidbody.MoveRotation (myRigidbody.rotation  Quaternion.Euler (Vector3.right  touch.deltaPosition.y  Time.deltaTime  5f));
        myRigidbody.MoveRotation (myRigidbody.rotation  Quaternion.Euler (Vector3.up  -touch.deltaPosition.x  Time.deltaTime  10f));

    }
}

在上面的每个块中都代表了限制 z 方向的独特努力。 现在请给我一些建议来实现同样的目标。

以及我在 Unity 论坛上的讨论 Touch based rotation of 3d model

编辑:我尝试过在 z 旋转中限制刚体。所以我的检查员看起来像这样。

编辑:在游戏开发聊天室讨论之后。我有以下类型的代码:

float prevZ = transform.eulerAngles.z;
transform.Rotate (Vector3.up, -touch.deltaPosition.x  Time.deltaTime  10f, Space.World);
transform.Rotate (Vector3.right, touch.deltaPosition.y  Time.deltaTime  5f, Space.World);

Vector3 modelVec = transform.eulerAngles;
modelVec.z = prevZ;
transform.eulerAngles = modelVec;

我刚刚接近解决方案,但现在我的高尔夫球球模型无法从顶部或底部侧阻力移动超过 180 度。我刚达到 180 度左右,它只是向任何其他方向旋转。

【问题讨论】:

  • 真的,我看不懂上面的链接。
  • 您能否更具体地说明“任何其他方向”的含义?
  • @AdamB,基本上我不想将地球仪旋转到垂直(z 轴)方向。
  • 是的,我明白了,但根据您的编辑,听起来您已经解决了这个问题。你编辑中的句子是什么意思

标签: c# unity3d rotation quaternions rigid-bodies


【解决方案1】:

好吧,在你的 cmets 之后,听起来你可能需要重新开始。

根据我的理解,试试这个:

Vector3 rotation = new Vector3();
rotation.y = -touch.deltaPosition.x * Time.deltaTime * yRotSpeed;
rotation.x = touch.deltaPosition.y * Time.deltaTime * xRotSpeed;
rotation.z = 0;
transform.Rotate(rotation);

然后我建议添加:

public float yRotSpeed;
public float xRotSpeed;

作为脚本的字段,以便您将来可以在统一引擎中调整旋转速度。

【讨论】:

    【解决方案2】:

    就这样吧。

     // private variables
    private Vector3 inputRotation;      // difference of input mouse pos and screen mid point
    private Vector3 mouseinput;
    
    // Update is called once per frame
    void Update() {
        FindPlayerInput();
    }
       void FindPlayerInput()
        {
        mouseinput = Input.mousePosition;
        mouseinput.z = 0; // for no rotation in z direction
        inputRotation = mouseinput - new Vector3(Screen.width * 0.5f, Screen.height * 0.5f,0);
        ProcessMovement();
        }
        void ProcessMovement(){
        transform.rotation = Quaternion.LookRotation(inputRotation);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      相关资源
      最近更新 更多