0
\$\begingroup\$

I want to know how can I change in unity the thershold of the method below:

transform.haschanged

At the moment it is so sensitive and it triggers with 0.00001 change in the camera.

We have created an Interactive VR video in unity and we want that everytime the user doesnt use the oculus for 30 seconds, a trailer video starts to play!

We have created the code below but even when the oculus is on the ground, still moves a bit (too sensitive):

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


public class Trailer : MonoBehaviour
{

public int timeOut;

private int timeOutTimer = 0;

private Quaternion gameObjectrotation;
private VideoPlayer videoPlayer;
public float dynamicRange = 100f;



// Use this for initialization
public void Awake()
{
    Vector3 lastPos;
    videoPlayer = GetComponentInParent<VideoPlayer>();
    gameObjectrotation = Camera.main.gameObject.transform.rotation;

    InvokeRepeating("ScreenSaver", 0, 1.0f);
}


public void ScreenSaver()
{

    timeOutTimer++;     

    var wMinus = gameObjectrotation.w - dynamicRange;
    var wPlus = gameObjectrotation.w + dynamicRange;
    var xMinus = gameObjectrotation.x - dynamicRange;
    var xPlus = gameObjectrotation.x + dynamicRange;
    var yMinus = gameObjectrotation.y - dynamicRange;
    var yPlus = gameObjectrotation.y + dynamicRange;
    var zMinus = gameObjectrotation.z - dynamicRange;
    var zPlus = gameObjectrotation.z + dynamicRange;

    var cameraW = Camera.main.gameObject.transform.rotation.w;
    var cameraX = Camera.main.gameObject.transform.rotation.x;
    var cameraY = Camera.main.gameObject.transform.rotation.y;
    var cameraZ = Camera.main.gameObject.transform.rotation.z;

    if (cameraW >= wMinus && cameraW <= wPlus && cameraX >= xMinus && cameraX <= xPlus && cameraY >= yMinus && cameraY <= yPlus && cameraZ >= zMinus && cameraZ <= zPlus && timeOutTimer >= timeOut)
    {
        //Show video
        videoPlayer.gameObject.SetActive(true);
        videoPlayer.Play();
    }
    else
    {
        //Hide Video           
        videoPlayer.gameObject.SetActive(false);
        videoPlayer.Stop();
        videoPlayer.frame = 1;
    }


    if (cameraW > wPlus || cameraW < wMinus || cameraX > xPlus || cameraX < xMinus || cameraY > yPlus || cameraY < yMinus || cameraZ > zPlus || cameraZ < zMinus)
    {
        timeOutTimer = 0;
    }



    gameObjectrotation = Camera.main.gameObject.transform.rotation;
}
}

Any help would be appreciated! Cheers

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the Game Development Stack Exchange! The Transform.HasChanged property/method is not called or defined in the above code from what I can tell. I'm sure if you add that the problem will be more obvious to readers. \$\endgroup\$
    – blurry
    Commented Jul 4, 2018 at 2:44

2 Answers 2

2
\$\begingroup\$

If you are using the Oculus SDK instead of OpenVR you can check with the proximity sensor of the oculus if somebody is wearing it or not. If I understood your use case right, this would be the preferred solution.

OVRManager.HMDMounted += HandleHMDMounted;
OVRManager.HMDUnmounted += HandleHMDUnmounted;

void HandleHMDMounted() {
   // Do stuff
}

void HandleHMDUnmounted() {
   // Do stuff
}

This worked for me on a Rift.

\$\endgroup\$
0
\$\begingroup\$

Reading the docs on transform.hasChanged, there is no threshold at all:

Note that operations which can change the transform will not actually check if the old and new value are different before setting this flag. So setting, for instance, transform.position will always set hasChanged on the transform, regardless of there being any actual change.

ie. this is just a dirty flag to help you skip unnecessary checks when the transform couldn't have changed, not a measure of how much it's changed.

So, if you want to look for significant changes, then this is not the correct tool for the job.

Instead, you're better off caching your last known position/orientation in your own script, and updating them / resetting your idle timer anytime the current value is more than a custom threshold away from that cached value.

eg.

public float translationThreshold = 0.001f;
public float rotationThreshold = 0.001f;

Vector3 lastPosition;
Quaternion lastRotation;
float idleSeconds;

void Update() {
    var camera = Camera.main.transform;

    bool moved = (camera.position - lastPosition).sqrMagnitude > translationThreshold;
    bool turned = Quaternion.Dot(camera.rotation, lastRotation) < (1f - rotationThreshold);

    if(moved | turned) {
        idleSeconds = 0f;
        lastPosition = camera.position;
        lastRotation = camera.rotation;
    } else {
        idleSeconds += Time.deltaTime;
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .