Unity常用射线检测接口用法及优缺点

在Unity中,射线检测是一种非常常见的技术,用于检测物体之间的碰撞、获取物体之间的距离等。今天我们来说一说射线检测的方法以及它们的优缺点:

Physics.Raycast:

using UnityEngine;

public class RaycastExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                Debug.Log("Hit object: " + hitInfo.collider.name);
            }
        }
    }
}

优点:

简单易用,适用于大多数场景。
可以检测单个射线与碰撞器之间的相交。

缺点:

只能检测单个射线,无法检测射线与多个碰撞器之间的相交。
需要额外处理射线方向和距离。

Physics.RaycastAll:

using UnityEngine;

public class RaycastAllExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit[] hitInfos = Physics.RaycastAll(ray);
            foreach (RaycastHit hitInfo in hitInfos)
            {
                Debug.Log("Hit object: " + hitInfo.collider.name);
            }
        }
    }
}

优点:

可以检测射线与多个碰撞器之间的相交。
返回所有相交的碰撞器,提供更多信息。

缺点:

需要额外处理返回的碰撞信息,包括距离、碰撞点等。

Physics.RaycastNonAlloc:

using UnityEngine;

public class RaycastNonAllocExample : MonoBehaviour
{
    RaycastHit[] hits = new RaycastHit[10]; // Pre-allocated array

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            int hitCount = Physics.RaycastNonAlloc(ray, hits);
            for (int i = 0; i < hitCount; i++)
            {
                Debug.Log("Hit object: " + hits[i].collider.name);
            }
        }
    }
}

优点:

与Physics.Raycast类似,但可以自定义返回结果数组,提高性能。

缺点:

需要手动管理返回结果数组,稍显复杂。

Physics.Linecast:

using UnityEngine;

public class LinecastExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Linecast(ray.origin, ray.origin + ray.direction * 100, out hitInfo))
            {
                Debug.Log("Hit object: " + hitInfo.collider.name);
            }
        }
    }
}

优点:

用于检测两个点之间的相交,比较适用于特定场景。

缺点:

只能检测直线路径上的相交,不能检测曲线路径。
需要提供起点和终点坐标。

Physics.BoxCast、Physics.SphereCast:

using UnityEngine;

public class BoxCastExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 origin = transform.position;
            Vector3 halfExtents = new Vector3(1, 1, 1);
            Vector3 direction = transform.forward;
            RaycastHit hitInfo;
            if (Physics.BoxCast(origin, halfExtents, direction, out hitInfo))
            {
                Debug.Log("Hit object: " + hitInfo.collider.name);
            }
        }
    }
}
using UnityEngine;

public class SphereCastExample : MonoBehaviour
{
    public float radius = 0.5f;
    public float distance = 10f;
    public LayerMask layerMask;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.SphereCast(ray, radius, out hitInfo, distance, layerMask))
            {
                Debug.Log("Hit object: " + hitInfo.collider.name);
            }
        }
    }
}

优点:

可以模拟物体的体积进行碰撞检测。

缺点:

比射线检测更消耗性能,适用于需要物体体积的检测场景。

选择适当的射线检测方法取决于你的具体需求和场景。在性能和功能之间进行权衡,根据实际情况选择最合适的方法。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦迪尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值