Search Unity

Question Unsmooth movement in AR

Discussion in 'AR' started by Majakr, Mar 20, 2023.

  1. Majakr

    Majakr

    Joined:
    Oct 17, 2022
    Posts:
    1
    Hello!
    I'm building an AR game, using AR Foundation and building for iOS. I'm trying to create a marker based system that tracks a marker and then uses its y-movement to make the player in the game jump. So the player will have a fixed x- and z-position. I've managed to track the marker and make the player move along the y-axis but the movement is very unpredictable and not smooth at all. It seems like the marker isn't tracked every frame, just every now and then. Does anyone know how to make this movement smooth?
    I'm using this script to track the movement and translate it to the player's movement, it's attached to an empty game object in the scene:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.XR.ARFoundation;
    4. using UnityEngine.XR.ARSubsystems;
    5.  
    6. public class MoveY : MonoBehaviour
    7. {
    8.     public ARTrackedImageManager imageManager;
    9.     public GameObject player;
    10.     public float yMin = 0f;
    11.     public float yMax = 3f;
    12.  
    13.     void OnEnable()
    14.     {
    15.         imageManager.trackedImagesChanged += OnTrackedImagesChanged;
    16.     }
    17.  
    18.     void OnDisable()
    19.     {
    20.         imageManager.trackedImagesChanged -= OnTrackedImagesChanged;
    21.     }
    22.  
    23.     void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    24.     {
    25.         foreach (ARTrackedImage trackedImage in eventArgs.updated)
    26.         {
    27.             if (trackedImage.trackingState == TrackingState.Tracking)
    28.             {
    29.                 // Get the y-position of the detected marker
    30.                 float markerYPosition = trackedImage.transform.position.y;
    31.                 // Use the y-position of the detected marker for any desired actions or calculations
    32.                 Debug.Log("Marker Y-position: " + markerYPosition);
    33.  
    34.                 // Calculate the y-position of the Player object within the desired range
    35.                 float yPosition = Mathf.Clamp((10 * markerYPosition), yMin, yMax);
    36.  
    37.                 // Set the y-position of the Player object
    38.                 Vector3 playerPosition = player.transform.position;
    39.                 playerPosition.y = yPosition;
    40.                 player.transform.position = playerPosition;
    41.  
    42.             }
    43.         }
    44.     }
    45. }
    46.  
     
  2. thomas_key

    thomas_key

    Unity Technologies

    Joined:
    Dec 14, 2019
    Posts:
    39
    Trackable update callback frequency is inherently unstable. What you probably want to do is assign an empty GameObject "Marker" to your tracked image, then smoothly interpolate your player's transform basis over time using
    Update()
    ,
    Time.deltaTime
    , and a smooth stepping function.
     
    Last edited: Apr 13, 2023
    andyb-unity likes this.