0

我正在尝试通过最小/最大限制来实现螺旋运动:

  • 正旋转>负变换
  • 负旋转>正变换

正负旋转都将通过 MRTK 手部交互提供的脚本完成,所以这不是我的问题。我(认为)我在逻辑上挣扎。

tl;dr:我想用 Unity、MRTK 和虚拟手来搞定一些事情。

4

1 回答 1

0

经过几天非常非常努力地挠头后,我最终解决了我的问题。老实说,我自己可能不会成功,因为逻辑问题(这种情况经常发生,不是吗?)。


最初设定

在我的情况下,与之交互的游戏对象是一个螺丝钉。我希望它在发生正旋转时执行正平移,反之亦然。在这个游戏对象上附加了一些组件:

  • 对撞机(网格、盒子、胶囊)
  • 刚体
  • 一个 PointerHandler.cs (MRTK)
  • 一个 ObjectManipulator.cs (MRTK)
  • NearInteractionGrabbable.cs (MRTK)
  • 下面找到的 TwistingRotation.cs

可以在此处找到刚体、PointerHandler.cs、ObjectManipulator.cs 和 TwistingRotation.cs 配置:

组件配置

魔术出现在 PointerHandler.cs 中,它允许我们检测何时与螺丝发生近距离交互,感谢 OnPointerDragged 事件。


TwistingRotation.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Translation of the object while rotating it when grabbed using the MRTK.ObjectManipulator
/// Boundaries and axis restrictions ongoing
/// </summary>

public class TwistingRotation : MonoBehaviour
{
    /*******CACHE REFERENCE*******/
    private Transform _myTransform;

    [SerializeField] private float translationFactor = 90f;

    private Vector3 _minPosition;
    private Vector3 _maxPosition;
    private Vector3 _previousVector;
    private Rigidbody _rb;


    private void Start()
    {
        // Cache reference
        _myTransform = gameObject.transform;
        _rb = gameObject.GetComponent<Rigidbody>();

        // Reference for the previous rotation vector
        _previousVector = _myTransform.up;
        // Default position is the maximum transform.position (unscrewed)
        _maxPosition = _myTransform.position;
        // Minimum position is default transform.position + 1unit in local space direction
        _minPosition = _maxPosition + Vector3.forward;
    }

    /// <summary>
    /// Move the object according to the rotation angle value
    /// A positive rotation leads to a positive translation, and vice-versa
    /// </summary>
    public void TranslateRotation()
    {
        // Retrieve the angle on a defined local axis when the rotation occur
        var currentVector = _myTransform.up;
        // Compute the angle between the previous and the current vector on a defined local axis
        // Difference between the previous rotation vector, and the actual, on a global axis
        var angle = Vector3.SignedAngle(_previousVector, currentVector, Vector3.forward);

        // Move object proportional to its rotation
        var translation = Vector3.forward * (angle / translationFactor);
        _myTransform.Translate(translation, Space.Self);

        // Get the GO current position
        var currentPosition = _myTransform.position;
        // Clamp for each axis between _minPosition and _maxPosition (the default spawn position)
        // Doing a Mathf.Min/Max inside the Mathf.Clamp to insure that the min and max values are correct 
        var x = Mathf.Clamp(currentPosition.x, Mathf.Min(_minPosition.x, _maxPosition.x), Mathf.Max(_minPosition.x, _maxPosition.x));
        var y = Mathf.Clamp(currentPosition.y, Mathf.Min(_minPosition.y, _maxPosition.y), Mathf.Max(_minPosition.y, _maxPosition.y));
        var z = Mathf.Clamp(currentPosition.z, Mathf.Min(_minPosition.z, _maxPosition.z), Mathf.Max(_minPosition.z, _maxPosition.z));

        // Compute the new position while taking the boundaries into consideration
        var newPosition = new Vector3(x, y, z);
        _myTransform.position = newPosition;

        // Save position for the next frame
        _previousVector = currentVector;
    }
}

怎么了 ?

很简单:螺杆在旋转时平移了 1 个(单位)单位。旋转的值取决于translationFactor 变量的值。在我的情况下,该值为 360,因此完全旋转 1(单位)单位平移。


结果

它远非完美,可能非常“meh”,但是嘿,它正在工作(不是预期的那样,但仍然如此),它让我继续前进,我做了我的演讲。

最后结果

于 2020-04-21T15:43:04.710 回答