Hi everyone,
I’m relatively new to AR development and followed this Unity documentation (AR Anchor Manager component | AR Foundation | 5.1.5) for anchoring objects in AR. However, I’m experiencing issues with my game’s stability.
When I test my game on my mobile device, the objects are not stable and tend to move with the camera (mobile) movement. Sometimes they even shift from one location to another far away, which is not expected behavior. Everything seems fine in the simulation, but the issues arise during actual mobile testing.
Here’s some relevant information:
- Device: Samsung Galaxy A34 (no depth sensor)
- Unity Version: 2022.3.15f1
- AR Foundation Version: 5.1.5
Here is the code snippet:
void Update()
{
if (!TryGetTouchPosition(out Vector2 touchPosition))
return;
if (isPlaneDetectionEnabled && m_RaycastManager.Raycast(touchPosition, s_Hits, TrackableType.PlaneWithinPolygon))
{
// Raycast hits are sorted by distance, so the first one will be the closest hit.
var hitPose = s_Hits[0].pose;
if (spawnedObject == null)
{
// Check if currentLevelIndex is within bounds before instantiating the prefab
if (currentLevelIndex >= 0 && currentLevelIndex < m_PlacedPrefabs.Length)
{
// Instantiate the prefab at the hit position and rotation
spawnedObject = Instantiate(m_PlacedPrefabs[currentLevelIndex], hitPose.position, hitPose.rotation);
// Apply rotation correction if needed
spawnedObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
characterController = spawnedObject.GetComponentInChildren<CharacterController>();
Debug.LogWarning(currentLevelIndex);
// Attach an ARAnchor component to the spawned object
objectAnchor = spawnedObject.AddComponent<ARAnchor>();
objectAnchor.transform.position = hitPose.position; // Ensure anchor is at the hit location
}
.
.
.
}
else
{
// Reposition the anchor
if (objectAnchor != null)
{
objectAnchor.transform.position = hitPose.position;
}
.
.
.
}
.
.
.
}
}
Thanks in advance.