Unity:在最小和最大距离之间旋转

3
我正在尝试实现拖动对象的功能。这个对象只能旋转一定角度(类似于门)。
以下是编辑过的代码,用于旋转对象,可以正常工作。我有两个向量来表示最大旋转角度和最小旋转角度。
每当用户拖动可交互对象时,将调用此代码(类似于更新但仅在拖动时)。
        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 }。应该可以工作!或者先检查旋转。 - Fredrik Schön
顺便问一下,“totransform” 不是打错了吧? - Fredrik Schön
啊,抱歉我没有详细说明,我会修改的。totransform是将被转换的对象的变换。 - Sjoerd Brauer
如果你正在开发一扇门,可以参考这个链接:http://stackoverflow.com/questions/37129162/open-a-door-front-and-back-in-unity3d-similar-to-amnesia/37133811#37133811。 - Programmer
2个回答

2

我认为你想要查看欧拉角。检查你得到的值,然后在旋转之前设置一个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。 - Sjoerd Brauer
啊,对了...检查器中的-1是359度。对于正数来说更有意义:http://imgur.com/a/l1qfC 嘿嘿。 - Fredrik Schön
欧拉角可以得到正确的角度,但是它们得出的数字可能与检查器中显示的不同。例如,逆时针旋转90度实际上是顺时针旋转270度,这就是从eulerangles获取的结果。建议使用transform.localEulerAngles。 - Fredrik Schön
嘿,谢谢你的提示,我认为这让我朝着正确的方向前进了。不知何故,localeulerangles还是一样的。 - Sjoerd Brauer
我找到了问题所在。似乎欧拉角有它自己的度数,就像你说的一样。我想我必须适应它,因为我找不到如何获得与检查器相同的值。感谢您让我走上了正确的轨道! - Sjoerd Brauer
不错!干得好!抱歉我不能提供更多帮助。 - Fredrik Schön

0

所以这是对我有效的解决方案。首先,我声明了移动变量(在下面没有显示,此处为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));
                }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接