使用oculus控制器围绕触摸位置旋转

时间:2019-05-20 10:33:47

标签: unity3d oculus

我想旋转一个附加到oculus控制器的对象(握住它,手动更新位置),但不应该是该控制器的子对象。

过程如下, 1.假设我有一个对象(枢轴(X,Y,Z)),我触摸了它(A,B,C)来抓取它,我是手动更新其位置,而不是使其成为控制器的子级。 2.现在,对象随控制器一起移动,想要计算其旋转度,以使其看起来像是已附加对象,并根据控制器更新其旋转度。

任何想法?

1 个答案:

答案 0 :(得分:0)

如果您不想让它仅仅是个孩子,我自然会看到3种选择:

1。使用FixedJoint

  

限制对象的移动依赖于另一个对象。这有点类似于育儿,但是是通过物理而非变换层次结构实现的。使用它们的最佳方案是,当您有想要轻松地彼此分离的对象,或无需父母就可以连接两个对象的运动时。

因此,每当您抓住物体时,都要做类似的事情

var fixedJoint = grabbedObject.AddComponent<FixedJoint>();
fixedJoint.connectedBody = controllerObject.GetComponent<RigidBody>();

//disable autoconfigure anchor
fixedJoint.autoConfigureConnectedAnchor = false;

// And set offsets depending on the current relative positions

// Get the position of the object in the controllers local coordinates
fixedJoint.connectedAnchor = controllerObject.transform.InverseTransformPoint(grabbedObject.transform.position);

// And get the controllers position relative to the object
fixedJoint.anchor = gtabbedObject.transform.InverseTransformPoint(controller.transform.posotion);

在释放对象的任何地方都应移除该关节,例如

Destroy(grabbedObject.GetComponent<FixedJoint>());

2。计算转换

获得相对位置偏移量后,您还可以使用一种伪造的育儿方法,只需使用控制器的局部坐标系简单地逐个智能地添加偏移量即可。

在可抓取的对象上具有类似的组件

public class CopyTransforms : MonoBehaviour
{
    private Transform target;
    private Transform startOffset;

    private RigidBody rb;

    private void Awake()
    {
        rb = GetComponent<RigidBody>();
    }

    // Call this In the moment you grab the object 
    public void Grab(Transform grabber)
    {
        target = grabber;
        offset = transform.position - target.position;
    }

    // Call this when releasing
    public void Release()
    {
        target = null;
    }

    // If dealing with rigidbodies you should use fixedupdate
    private void FixedUpdate()
    {
        if(!target) return;

        // Apply rotation
        rb.MoveRotation(target.rotation);

        var targetPosition = target.position + target.right * offset.x + target.up * offset.y + target.forward * target.z;
        rb.MovePosition(targetPosition);
    }
}

3。使用假父母育儿

这个想法是在控制器下面有一个空对象。

在抓取的那一刻,便将此空对象的位置设置为抓取对象的位置->以后,每当控制器移动时,此空对象便会成为抓取对象的位置。所以您只需要应用它的变换并完成

var emptyObject = new GameObject("empty");
emptyObject.transform.SetParent(controllerObject.trasform, false);

emptyObject.transform.rotation = grabbedObject.transform.rotation;
emptyObject.transform.position = grabbedObject.transform.position;

现在您只需复制其变换,例如在

void Update()
{
    grabbedObject.transform.rotation = emptyObject.transform.rotation;

    grabbedObject.transform.position = emptyObject.transform.position;
}

或像以前一样在FixedUpdate中。


注意:请在智能手机上输入内容,因此没有保修,但我希望这个想法会清楚。