【问题标题】:Setup an helical movement when interacting using the MRTK使用 MRTK 交互时设置螺旋运动
【发布时间】:2020-04-21 23:43:04
【问题描述】:

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

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

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

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

【问题讨论】:

  • 那么,在实施你的想法时,阻碍你的实际问题是什么?您可以提供有关您的项目和遇到的问题的更具体信息,最好添加一些代码来说明问题。
  • 嘿@Hernando-MSFT!很抱歉错过了您的评论!我找到了解决方案并在下面进行了解释。无论如何谢谢!

标签: unity3d rotation hololens mrtk windows-mixed-reality


【解决方案1】:

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


初始设置

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

  • 对撞机(网格、盒子、胶囊)
  • 刚体
  • 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”,但嘿,它正在工作(虽然不是预期的那样,但仍然),它让我继续前进,我做了我的演示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多