【问题标题】:How can I trigger an event when the user rotates an object with his hands using Leap Motion in Virtual Reality?当用户在虚拟现实中使用 Leap Motion 用手旋转对象时,如何触发事件?
【发布时间】:2019-03-12 18:07:42
【问题描述】:

我是虚拟现实的新手。我将 Oculus Rift 用于耳机,使用 Leap Motion 进行交互。当用户用手旋转一个对象时,我希望触发一个特定的事件。

这是我的代码:

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

public class step1 : MonoBehaviour
{

    public GameObject object;
    public ParticleSystem event;


    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if(object.transform.rotation == Quaternion.AngleAxis(-30,Vector3.right))
        {
            Debug.Log("Done");
            event.Play();   
        }
    }
}

【问题讨论】:

    标签: unity3d virtual-reality oculus leap-motion


    【解决方案1】:

    请注意,由于四元数可以表示最多两整圈(720 度)的旋转,因此即使结果旋转看起来相同,此比较也可能返回 false。

    来自Quaternion.operator == Unity Docs

    我会避免一起使用四元数,因为它们很难缠住你的头并且使用起来很笨重。

    尝试将Vector3 表示与eulerAngles 一起使用,然后测试近似等于值,如下所示:

    //only checks for one axis!
    if(Math.Abs(rotationA.eulerAngles.x - rotationB.eulerAngles.x) <= maxDifference)
    {
        //do stuff
    }
    

    或者坚持使用Quaternion.Angle,但是像这样使用它:

    //compares angle directly
    if(Math.Abs(Quaternion.Angle(rotationA, rotationB)) <= maxDifference)
    {
        //do stuff
    }
    

    Vector3内部由三个float 值组成,Quaternion.Angle 返回一个float 值。在 99% 的情况下,比较它们是否完全相等是行不通的。将它们与您可以接受的最大差异进行比较,它应该可以工作。

    【讨论】:

    • 或者你可以使用Mathf.Approximately来比较floats
    • 当然。这可能是大多数情况下的最佳解决方案。我只是想控制我的比较返回 true 的边距,因此 maxDifference 比较。我认为Mathf.Approximately 不可能做到这一点?不确定。
    【解决方案2】:

    你想触发这个事件

    1. 当用户第一次开始旋转对象时
    2. 在用户旋转对象时持续触发
    3. 当用户完成旋转对象时

    顺便说一句,您不必只选择其中之一

    目前您的实现只会在您的对象具有特定旋转时触发事件,这是什么预期行为?

    【讨论】:

    • 这更像是一个问题而不是一个答案。
    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多