I have to build an app in AR, with a smartphone I should be able to see an object stick in a real world position, for all the user the same thing in the same place.
I have to use just Geospatial Anchor or I have to use also a Cloud Anchor. Someone can suggest something?
I try with this script to Instantiate a Geospatial anchor and a prefab marker, but the marker it's constantly updating its position and it's nowhere near the coordinates I entered.
`
private void Update()
{
if (ARSession.state != ARSessionState.SessionInitializing &&
ARSession.state != ARSessionState.SessionTracking)
{
return;
}
featureSupport = arEarthManager.IsGeospatialModeSupported(GeospatialMode.Enabled);
if (featureSupport != FeatureSupported.Supported)
return;
isSessionReady = ARSession.state == ARSessionState.SessionTracking &&
Input.location.status == LocationServiceStatus.Running;
earthTrackingState = arEarthManager.EarthTrackingState;
if (isSessionReady && earthTrackingState == TrackingState.Tracking)
{
cameraGeospatialPose = arEarthManager.CameraGeospatialPose;
if (anchor == null)
{
counterSpawn++;
anchor = ARAnchorManagerExtensions.AddAnchor(arAnchorManager, latitude, longitude, altitude, Quaternion.identity);
spawnedPrefab = Instantiate(prefabMarker, anchor.transform);
}
}
}
`
You should use Geospatial anchor for this.
To placing the ancor use the following code (note the change from AddAnchor
to ResolveAnchorOnTerrain
):
[SerializeField] private ARAnchorManager _anchorManager;
private void Update()
{
// Your code came here
StartCoroutine(PlaceTerrainAnchor(latitude, longitude, altitude));
}
private IEnumerator PlaceTerrainAnchor(double latitude, double longitude, float altitude)
{
anchor = _anchorManager.ResolveAnchorOnTerrain(latitude, longitude, altitude, Quaternion.identity);
while (anchor.terrainAnchorState == TerrainAnchorState.TaskInProgress) yield return null;
spawnedPrefab = Instantiate(prefabMarker, anchor.transform);
}
If your marker is still not placed correctly, make sure you set the coordinates in the proper format, and also check for the actual accuracy of the tracking with the following codes:
[SerializeField] private AREarthManager _earthManager;
private void Update()
{
if (_earthManager.EarthTrackingState != TrackingState.Tracking) Debug.Log($"Tracking not established. Current state: {_earthManager.EarthTrackingState}");
else Debug.Log($"Horizontal accuracy: {_earthManager.CameraGeospatialPose.HorizontalAccuracy}, Vertical accuracy: {_earthManager.CameraGeospatialPose.VerticalAccuracy}, Heading accuracy: {_earthManager.CameraGeospatialPose.HeadingAccuracy}");
}