I am working with an AR project rn. It detects an image and as an overlay spawns a cube ( just to test if it’s working ), but when i started testing I found out that the cube is spawning with an offset. Can someone explain how to fix this problem? Block with a texture AR Session Origin settings The photo of the problem
And my anchor script:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ARObjectSpawner : MonoBehaviour
{
public GameObject prefab;
private ARRaycastManager raycastManager;
private ARAnchorManager anchorManager;
private List<ARRaycastHit> hits = new List<ARRaycastHit>();
void Start()
{
raycastManager = FindObjectOfType<ARRaycastManager>();
anchorManager = FindObjectOfType<ARAnchorManager>();
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
if (raycastManager.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon))
{
Pose hitPose = hits[0].pose;
GameObject spawnedObject = Instantiate(prefab, hitPose.position, hitPose.rotation);
ARAnchor anchor = anchorManager.AddAnchor(new Pose(hitPose.position, Quaternion.identity));
if (anchor != null)
{
spawnedObject.transform.SetParent(anchor.transform);
}
else
{
Debug.Log("Failed to create anchor.");
}
}
}
}
}
}
How could I make a cube spawn DIRECTLY on the image? Without an offset
1