Unity3D笔记 英保通十 射线碰撞器检测
射线碰撞检测可以用来检测方向和距离: 通过Physics.RayCast光线投射来实现:常用于射击利用发射的射线来判断、还有对战中刀剑交战中。。
一、要涉及到RayCast和RayCastHit
1.1、光线投射:Physics.RayCast()
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
返回值也是bool
-
origin:在世界坐标,射线的起始点。
- direction:射线的方向
- distance:射线的长度
- layerMask:只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
1.2、RayCastHit() 光线投射检测
用来获取从raycast函数中得到的信息反馈的结构。
二、案例
using UnityEngine; using System.Collections; public class rayCast : MonoBehaviour { private RaycastHit rayHit;//光线投射碰撞:用来获取从raycast函数中得到的信息反馈的结构。 // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Physics.Raycast(transform.position, transform.forward,out rayHit, 10)) { Debug.Log("碰撞检测到第一人称碰撞到了"+rayHit.collider.name); } } }
RayCastHit参数:
-
The impact point in world space where the ray hit the collider.
在世界空间中,射线碰到碰撞器的碰撞点。 -
The normal of the surface the ray hit.
射线所碰到的表面的法线。 -
The barycentric coordinate of the triangle we hit.
所碰到的三角形的重心坐标。 -
The distance from the ray's origin to the impact point.
从光线的原点到碰撞点的距离。 -
The index of the triangle that was hit.
碰到的三角形的索引。 -
The uv texture coordinate at the impact point.
在碰撞点的UV纹理坐标。 -
The secondary uv texture coordinate at the impact point.
碰撞点的第二个UV纹理坐标。 -
The uv lightmap coordinate at the impact point.
所在碰撞点的光照图UV坐标。 -
The Collider that was hit.
碰到的碰撞器。 -
The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.
碰到的碰撞器的Rigidbody。如果该碰撞器没有附加刚体那么它为null。 -
The Transform of the rigidbody or collider that was hit.
碰到的刚体或碰撞器的变换。
作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述