Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to fix GvrHead.cs script to prevent it to crash AR/VR app' ?

Discussion in 'AR/VR (XR) Discussion' started by Deleted User, Feb 2, 2017.

  1. Deleted User

    Deleted User

    Guest

    I found where the problem comes from, the GvrHead.cs script that crashes the application.

    To be compatible with the Vuforia package (Augmented reality), the GvrHead.cs is modified : How to Integrating Cardboard to the AR/VR Sample
    It is this modification that crashes the application, with the basic script everything works well !

    GvrHead.cs modified to be compatible with the vuforia package
    Code (CSharp):
    1.     if (OnHeadUpdated != null) {
    2.       OnHeadUpdated(gameObject);
    3.     }
    4.         // The part of the code added to the base script
    5.         Vuforia.VuforiaBehaviour.Instance.UpdateState(false, true);
    Now my question is: how to make this error disappear ?

    Capture.JPG

    Code (CSharp):
    1. // Copyright 2014 Google Inc. All rights reserved.
    2. //
    3. // Licensed under the Apache License, Version 2.0 (the "License");
    4. // you may not use this file except in compliance with the License.
    5. // You may obtain a copy of the License at
    6. //
    7. //     http://www.apache.org/licenses/LICENSE-2.0
    8. //
    9. // Unless required by applicable law or agreed to in writing, software
    10. // distributed under the License is distributed on an "AS IS" BASIS,
    11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. // See the License for the specific language governing permissions and
    13. // limitations under the License.
    14.  
    15. using UnityEngine;
    16.  
    17. /// This script provides head tracking support for a camera.
    18. ///
    19. /// Attach this script to any game object that should match the user's head motion.
    20. /// By default, it continuously updates the local transform to GvrViewer.HeadView.
    21. /// A target object may be specified to provide an alternate reference frame for the motion.
    22. ///
    23. /// This script will typically be attached directly to a _Camera_ object, or to its
    24. /// parent if you need to offset the camera from the origin.
    25. /// Alternatively it can be inserted as a child of the _Camera_ but parent of the
    26. /// GvrEye camera.  Do this if you already have steering logic driving the
    27. /// mono Camera and wish to have the user's head motion be relative to that.  Note
    28. /// that in the latter setup, head tracking is visible only when VR Mode is enabled.
    29. ///
    30. /// In some cases you may need two instances of GvrHead, referring to two
    31. /// different targets (one of which may be the parent), in order to split where
    32. /// the rotation is applied from where the positional offset is applied.  Use the
    33. /// #trackRotation and #trackPosition properties in this case.
    34. [AddComponentMenu("GoogleVR/GvrHead")]
    35. public class GvrHead : MonoBehaviour {
    36.   /// Determines whether to apply the user's head rotation to this gameobject's
    37.   /// orientation.  True means to update the gameobject's orientation with the
    38.   /// user's head rotation, and false means don't modify the gameobject's orientation.
    39.   public bool trackRotation = true;
    40.  
    41.   /// Determines whether to apply ther user's head offset to this gameobject's
    42.   /// position.  True means to update the gameobject's position with the user's head offset,
    43.   /// and false means don't modify the gameobject's position.
    44.   public bool trackPosition = true;
    45.  
    46.   /// The user's head motion will be applied in this object's reference frame
    47.   /// instead of the head object's parent.  A good use case is for head-based
    48.   /// steering.  Normally, turning the parent object (i.e. the body or vehicle)
    49.   /// towards the direction the user is looking would carry the head along with it,
    50.   /// thus creating a positive feedback loop.  Use an external target object as a
    51.   /// fixed point of reference for the direction the user is looking.  Often, the
    52.   /// grandparent or higher ancestor is a suitable target.
    53.   public Transform target;
    54.  
    55.   /// Determines whether the head tracking is applied during `LateUpdate()` or
    56.   /// `Update()`.  The default is false, which means it is applied during `LateUpdate()`
    57.   /// to reduce latency.
    58.   ///
    59.   /// However, some scripts may need to use the camera's direction to affect the gameplay,
    60.   /// e.g by casting rays or steering a vehicle, during the `LateUpdate()` phase.
    61.   /// This can cause an annoying jitter because Unity, during this `LateUpdate()`
    62.   /// phase, will update the head object first on some frames but second on others.
    63.   /// If this is the case for your game, try switching the head to apply head tracking
    64.   /// during `Update()` by setting this to true.
    65.   public bool updateEarly = false;
    66.  
    67.   /// Returns a ray based on the heads position and forward direction, after making
    68.   /// sure the transform is up to date.  Use to raycast into the scene to determine
    69.   /// objects that the user is looking at.
    70.   public Ray Gaze {
    71.     get {
    72.       UpdateHead();
    73.       return new Ray(transform.position, transform.forward);
    74.     }
    75.   }
    76.  
    77.   public delegate void HeadUpdatedDelegate(GameObject head);
    78.  
    79.   /// Called after the head pose has been updated with the latest sensor data.
    80.   public event HeadUpdatedDelegate OnHeadUpdated;
    81.  
    82.   void Awake() {
    83.     GvrViewer.Create();
    84.   }
    85.  
    86.   private bool updated;
    87.  
    88.   void Update() {
    89.     updated = false;  // OK to recompute head pose.
    90.     if (updateEarly) {
    91.       UpdateHead();
    92.     }
    93.   }
    94.  
    95.   // Normally, update head pose now.
    96.   void LateUpdate() {
    97.     UpdateHead();
    98.   }
    99.  
    100.   // Compute new head pose.
    101.   private void UpdateHead() {
    102.     if (updated) {  // Only one update per frame, please.
    103.       return;
    104.     }
    105.     updated = true;
    106.     GvrViewer.Instance.UpdateState();
    107.  
    108.     if (trackRotation) {
    109.       var rot = GvrViewer.Instance.HeadPose.Orientation;
    110.       if (target == null) {
    111.         transform.localRotation = rot;
    112.       } else {
    113.         transform.rotation = target.rotation * rot;
    114.       }
    115.     }
    116.  
    117.     if (trackPosition) {
    118.       Vector3 pos = GvrViewer.Instance.HeadPose.Position;
    119.       if (target == null) {
    120.         transform.localPosition = pos;
    121.       } else {
    122.         transform.position = target.position + target.rotation * pos;
    123.       }
    124.     }
    125.  
    126.     if (OnHeadUpdated != null) {
    127.       OnHeadUpdated(gameObject);
    128.     }
    129.   }
    130. }

     
    Last edited by a moderator: Feb 2, 2017
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  3. Deleted User

    Deleted User

    Guest

    Thank you mgear, with the searches I just made : I can use logcat with Android Studio to test my .apk file ? It's a kind of emulator ? But all the adjustments to make look very complex...

    What do I have to do to understand where my problem comes from ?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    no, connect the phone to your pc, then run that command in command prompt,
    start your app in phone, then can see device log. (probably some error messages outputted there)
     
  5. Deleted User

    Deleted User

    Guest

    I found how to use "adb logcat" with this video and here is the result :

    adb.png

    I don't think it's the Gvrhead that crashes the application since until now this was no problem... Do you have an explanation with this screenshot ? :)