ARFoundation快速入门-13Raycasting

一、Raycasting简介

Raycasting:直译为射线投射,指的是从三维空间,往一个方向发射一条无限长的射线,碰撞到平面就返回碰撞到平面的集合。

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

/// <summary>射线投射测试</summary>
[RequireComponent(typeof(ARRaycastManager))]
public class ARRaycastTest : MonoBehaviour
{
    private static List<ARRaycastHit> Hits;
    private ARRaycastManager mRaycastManager;

    private void Start()
    {
        Hits = new List<ARRaycastHit>();
        mRaycastManager = GetComponent<ARRaycastManager>();
    }

    //从屏幕坐标中发射一条射线,检测是否碰撞到物体,返回碰撞到物体的位置信息
    public bool Detect(Vector2 touchPs,out Vector3 ps)
    {
        if (mRaycastManager.Raycast(touchPs, Hits, TrackableType.PlaneWithinPolygon | TrackableType.PlaneWithinBounds))
        {
            var hitPose = Hits[

猜你喜欢

转载自blog.csdn.net/a451319296/article/details/106801856