2

I am new to Virtual Reality. I am using Oculus Rift for Headset and Leap Motion for interactivity. When the user will rotate an object with his hands, I want a specific event to get triggered.

Here's my code:

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();   
        }
    }
}
simmy
  • 19
  • 5

2 Answers2

1

Note that because quaternions can represent rotations that are up to two full revolutions (720 degrees), this comparison can return false even if resulting rotations look the same.

From Quaternion.operator == Unity Docs

I would avoid working with quaternions alltogether since they are a pain to wrap your head around and clunky to use.

Try using a Vector3 representation with eulerAngles and then test for a approximate equals value something like this:

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

Or stick with Quaternion.Angle but use it like this:

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

Vector3consists of three float values internally and Quaternion.Angle returns a float value. Comparing them for exact equality is not going to work in 99% of all cases. Compare them to a maximum difference you are ok with and it should work.

Nicolas
  • 440
  • 4
  • 13
  • 1
    Or you can use [`Mathf.Approximately`](https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html) for comparing the `floats` – derHugo Mar 15 '19 at 07:04
  • Sure. That would propably be the optimal solution for most cases. I just like to have control over the margin where my comparison returns true, hence the maxDifference comparison. I think this is not possible with `Mathf.Approximately`? Not sure tho. – Nicolas Mar 15 '19 at 08:42
0

Do you want to fire the event

  1. when the user first starts rotating the object
  2. continually fire while the user is rotating the object
  3. when the user is done rotating the object

you don't have to pick just one of these by the way

currently your implementation will only fire the event when your object has a specific rotation is that what expected behavior?

BrightenedLight
  • 333
  • 1
  • 6