【问题标题】:unity Rotate between min and max distanceunity 在最小和最大距离之间旋转
【发布时间】:2017-02-16 14:43:29
【问题描述】:

我正在尝试使拖动对象成为可能。这个物体只能旋转这么多。 (类似于门)。

这是 abit 编辑的代码,用于旋转有效的对象。 我有 2 个用于最大旋转和最小旋转的向量。

每当用户拖动可交互对象时,都会调用此代码。 (类似于更新,但仅在拖动时)

        if (GestureManager.Instance.IsNavigating &&
    HandsManager.Instance.FocusedGameObject == gameObject)
        {
            //speed and navigiation of rotation
            float rotationFactor;
            rotationFactor = ManipulationManager.Instance.ManipulationPosition.y * RotationSensitivity;
            totransform.Rotate(new Vector3(rotationFactor, 0, 0));
        }

如果我可以在这里使用 if 语句,那就太好了。我尝试了很多东西,但它仍然无法正常工作。

如上所述,此处的代码粘贴有效。对象应该是可拖动的,但只能拖动到特定点。

totransform 是要旋转的变换

任何想法都会非常好,非常感谢。

亲切的问候。

【问题讨论】:

  • 嗯,你尝试了什么?您可以在 totransform.Rotate 之前执行Debug.Log(rotationFactor); 并复制所需的最大和最小位置上的值,然后执行if (withinThatRange) { Rotate }。应该管用!或者更确切地说,先检查旋转。
  • 顺便说一句,“totransform”不是错字,对吧?
  • 对不起,我没有详细说明,我会改变它。 totransform 是将要转换的对象的转换。
  • 如果你在做门,看看this

标签: c# unity3d rotation quaternions hololens


【解决方案1】:

我想你想看看eulerAngles。检查您获得的值,然后在进行轮换之前设置一个 if 语句。这是一个示例代码,供您查找所需的值:

if (GestureManager.Instance.IsNavigating &&
    HandsManager.Instance.FocusedGameObject == gameObject)
{
    //speed and navigiation of rotation
    float rotationFactor = ManipulationManager.Instance.ManipulationPosition.y * RotationSensitivity;

    Debug.Log(totransform.eulerAngles);
    if (totransform.eulerAngles.x < 100) {
        totransform.Rotate(new Vector3(rotationFactor, 0, 0));
    }
}

【讨论】:

  • 因此,在这种情况下,假设 x = -90 的最小旋转和最大 0。调试时我检查 eulerangle 中 X 的值,它转换为像 3 这样的数字。而原始 x旋转为-80。但只有在运行应用程序后,它才会在调用 eulerangle 时变为 3
  • 啊,对... -1 在检查器中是 359 度。对于正数,它更有意义:imgur.com/a/l1qfChehe。
  • 好吧,欧拉会为您提供正确的角度,但它不会与检查器中的数字相同。例如,逆时针 -90 度是顺时针 270 度,这就是你从欧拉角得到的。使用 transform.localEulerAngles。
  • 嘿,谢谢你的提示,我认为这让我朝着正确的方向前进。出于某种原因,localeulerangles 是相同的。
  • 我发现了问题。似乎eulerangle有它自己的度数,就像你说的那样。我想我必须适应这一点,因为我找不到与检查员相同的值。感谢您让我走上正轨!
【解决方案2】:

所以这是对我有用的解决方案。首先我声明了运动变量(下面没有看到,在这种情况下是 2)。然后我跟踪所覆盖的距离并对其进行限制。

当然,这段代码有一些改进,比如使用移动而不是 2。但由于时间限制,我没有这样做。

    if (GestureManager.Instance.IsNavigating &&
    HandsManager.Instance.FocusedGameObject == gameObject)
        {

                //here we get the movement direction and set it in movement.
                if (GestureManager.Instance.NavigationPosition.y > 0)
                {
                    movement = 2;
                }
                else if (GestureManager.Instance.NavigationPosition.y < 0)
                {
                    movement = -2;
                }


            //the first part is false if we reach higher then maxdistance and the movement is going up
            //the second part is false if we reach the lower distance and the movement is going down.
            if ((!(distance > maxdistance.x) || movement < 0) && ((!(distance < mindistance.x) || movement > 0)))
                {
                    //here we add the movement to the distance so we know if it gets closer or further
                    distance += movement;
                    //here we rotate
                    totransform.Rotate(new Vector3(movement, 0, 0));
                }
        }

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2019-01-26
    • 1970-01-01
    • 2014-03-31
    • 2011-03-03
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多