Search Unity

  1. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Bug MakeContentAppearAt does not work every frame when trying to match a rotation

Discussion in 'AR' started by devpsst, Sep 2, 2021.

  1. devpsst

    devpsst

    Joined:
    Feb 10, 2021
    Posts:
    1
    We are trying to get some piece of content to appear on an ARPlane based on where the device is looking, and to also rotate the content so it is facing the user.

    To do this we are using MakeContentAppearAt, however when providing a rotation every frame into this method it starts to jitter the further away you look from the starting rotation.

    What is the best approach to handle this? Example code attached, taken from the ARFoundation Samples repo and modified to match a rotation facing the camera.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using Mark.Managers.Core;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.XR.ARSubsystems;
    6.  
    7. namespace UnityEngine.XR.ARFoundation.Samples
    8. {
    9.     /// <summary>
    10.     /// Moves the ARSessionOrigin in such a way that it makes the given content appear to be
    11.     /// at a given location acquired via a raycast.
    12.     /// </summary>
    13.     [RequireComponent(typeof(ARSessionOrigin))]
    14.     [RequireComponent(typeof(ARRaycastManager))]
    15.     public class MakeAppearOnPlane : MonoBehaviour
    16.     {
    17.         [SerializeField]
    18.         [Tooltip("A transform which should be made to appear to be at the touch point.")]
    19.         Transform m_Content;
    20.  
    21.         /// <summary>
    22.         /// A transform which should be made to appear to be at the touch point.
    23.         /// </summary>
    24.         public Transform content
    25.         {
    26.             get { return m_Content; }
    27.             set { m_Content = value; }
    28.         }
    29.  
    30.         [SerializeField]
    31.         [Tooltip("The rotation the content should appear to have.")]
    32.         Quaternion m_Rotation;
    33.  
    34.         /// <summary>
    35.         /// The rotation the content should appear to have.
    36.         /// </summary>
    37.         public Quaternion rotation
    38.         {
    39.             get { return m_Rotation; }
    40.             set
    41.             {
    42.                 m_Rotation = value;
    43.                 if (m_SessionOrigin != null)
    44.                     m_SessionOrigin.MakeContentAppearAt(content, content.transform.position, m_Rotation);
    45.             }
    46.         }
    47.  
    48.         void Awake()
    49.         {
    50.             m_SessionOrigin = GetComponent<ARSessionOrigin>();
    51.             m_RaycastManager = GetComponent<ARRaycastManager>();
    52.         }
    53.  
    54.         void Update()
    55.         {
    56.             if (Input.touchCount == 0 || m_Content == null)
    57.                 return;
    58.  
    59.             var touch = Input.GetTouch(0);
    60.  
    61.             if (m_RaycastManager.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinPolygon))
    62.             {
    63.                 // Raycast hits are sorted by distance, so the first one
    64.                 // will be the closest hit.
    65.                 var hitPose = s_Hits[0].pose;
    66.  
    67.                 var camFwd = Camera.main.transform.forward;
    68.                 camFwd.y = 0f;
    69.                 var rot = Quaternion.LookRotation(camFwd);
    70.  
    71.                 // This does not move the content; instead, it moves and orients the ARSessionOrigin
    72.                 // such that the content appears to be at the raycast hit position.
    73.                 m_SessionOrigin.MakeContentAppearAt(content, hitPose.position, rot);
    74.             }
    75.         }
    76.  
    77.         static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
    78.  
    79.         ARSessionOrigin m_SessionOrigin;
    80.  
    81.         ARRaycastManager m_RaycastManager;
    82.     }
    83. }
     
unityunity