Unsmooth movement in AR

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:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class MoveY : MonoBehaviour
{
    public ARTrackedImageManager imageManager;
    public GameObject player;
    public float yMin = 0f;
    public float yMax = 3f;

    void OnEnable()
    {
        imageManager.trackedImagesChanged += OnTrackedImagesChanged;
    }

    void OnDisable()
    {
        imageManager.trackedImagesChanged -= OnTrackedImagesChanged;
    }

    void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    {
        foreach (ARTrackedImage trackedImage in eventArgs.updated)
        {
            if (trackedImage.trackingState == TrackingState.Tracking)
            {
                // Get the y-position of the detected marker
                float markerYPosition = trackedImage.transform.position.y;
                // Use the y-position of the detected marker for any desired actions or calculations
                Debug.Log("Marker Y-position: " + markerYPosition);

                // Calculate the y-position of the Player object within the desired range
                float yPosition = Mathf.Clamp((10 * markerYPosition), yMin, yMax);

                // Set the y-position of the Player object
                Vector3 playerPosition = player.transform.position;
                playerPosition.y = yPosition;
                player.transform.position = playerPosition;

            }
        }
    }
}

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.

1 Like