开发环境
Unity:2021.1.14flc1 导入Package: AR Foundation:4.1.7 ARCore XR Plugin:4.1.7
持续射线投射案例
制作一个随着镜头移动的位于屏幕中心的平面
步骤
注意要提前配置好AR Raycast Manager才可以使用 可以参考ARFoundation入门——AR射线检测(Android)
-
在AR场景中创建一个Plane,这里平面不要设置太大,影响观察,我设置的Scale是(0.01,0.01,0.01) -
在AR Session Oringin中创建脚本:
public class MonoBehaviour_01 : MonoBehaviour
{
public GameObject defaultPrefab;
private ARRaycastManager raycastManager;
private List<ARRaycastHit> hits = new List<ARRaycastHit>();
public Transform target;
void Start()
{
raycastManager = GetComponent<ARRaycastManager>();
}
void Update()
{
if (raycastManager.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.PlaneWithinPolygon))
{
hits.Sort((x,y)=>x.distance.CompareTo(y.distance));
target.position = hits[0].pose.position;
target.rotation = hits[0].pose.rotation;
}
}
}
- 将Plane拖入到AR Session Oringin 脚本控件的 Target中
一些说明
对ARFoundation的使用学习可以参考unity官网的文档:http://docs.unity3d.com/Packages/com.unity.xr.arfoundation@4.1/manual/index.html。也可以到ARCore的官网:https://developers.google.cn/ar对这部分SDK的功能和环境配置有个更好的了解。 (两个网站的中文翻译我觉得都挺令人头疼的,看不懂的部分地方建议可以用英文文档理解一下。)
|