我最终解决了我的问题,经过几天真的非常非常努力地挠头。
老实说,我自己可能不会成功,因为逻辑问题(这种情况经常发生,不是吗?)。
初始设置
在我的例子中,与之交互的游戏对象是一个螺丝钉。
我希望它在发生正向旋转时执行正向平移,反之亦然。
在这个游戏对象上附加了一些组件:
- 对撞机(网格、盒子、胶囊)
- 刚体
- 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”,但嘿,它正在工作(虽然不是预期的那样,但仍然),它让我继续前进,我做了我的演示。