'In AR Foundation, Can the object be automatically created without touching it?
In ARFoundation - ARCore
When I hold my phone without touching the screen, can I automatically recognize the plane and make the object appear?
And make the object move alone? (self-moving)
Solution 1:[1]
Yes, you can absolutely do that.
1. Detect plane automatically:
Add AR Plane Manager script to your AR Session Origin
2. Place object automatically:
First way, use raycast from center of your screen
ARRaycastManager raycastManager;
List<ARRaycastHit> hitList = new List<ARRaycastHit>();
Vector2 centerOfScreen = new Vector2(Screen.width / 2, Screen.height / 2);
raycastManager.Raycast(centerOfScreen, hitList, TrackableType.Planes);
if (hitList.Count > 0)
{
// change it to make fit your logics
Instantiate(objectPrefab, hitList[0].pose.position, hitList[0].pose.position);
}
Second way, you can use event planesChanged of ARPlaneManager to do it.
ARPlaneManager arPlaneManager
void OnEnable()
{
...
arPlaneManager.planesChanged += PlaneChanged;
}
void OnDisable()
{
...
arPlaneManager.planesChanged -= PlaneChanged;
}
private void PlaneChanged(ARPlanesChangedEventArgs args)
{
if (args.added != null)
{
ARPlane arPlane = args.added[0];
// change it to make fit your logics
Instantiate(objectPrefab, arPlane.transform.position, Quaternion.identity);
}
}
Reference link: https://www.youtube.com/watch?v=oBKrdRI_NGI
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
