Unity基础篇:射线检测Physics.Raycast相关功能整合。

我们先看射线的相关文档。

public static bool RayCast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

public static bool Raycast(Ray ray, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

Parameters 参数

originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。
directionThe direction of the ray.
射线的方向。
distanceThe length of the ray.
射线的长度。
layermaskA Layer mask that is used to selectively ignore colliders when casting a ray.
投射射线,选择投射的层蒙版。
queryTriggerInteraction

Specifies whether this query should hit Triggers.
指定是否查询碰到触发器。

 

rayThe starting point and direction of the ray.
射线的起点和方向

 

hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
hitInfo将包含碰到器碰撞的更多信息。

 

RaycastHit 射线投射碰撞信息

struct in UnityEngine

Description 描述

Structure used to get information back from a raycast.

该结构用来获取射线投射返回的信息。

See Also: Physics.Raycast, Physics:Physics.Linecast, Physics.RaycastAll.

Variables 变量

barycentricCoordinateThe barycentric coordinate of the triangle we hit.
碰到的三角形的重心坐标。
colliderThe Collider that was hit.
碰到的碰撞器。
distanceThe distance from the ray's origin to the impact point.
从射线的原点到触碰点的距离。
lightmapCoordThe uv lightmap coordinate at the impact point.
在触碰点的UV光照贴图的坐标。
normalThe normal of the surface the ray hit.
射线触碰表面的法线。
pointThe impact point in world space where the ray hit the collider.
在世界坐标空间,射线碰到碰撞器的接触点。
rigidbodyThe Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.
碰到的该碰撞器上的刚体。如果碰撞器上没有附加刚体,那么返回null。
textureCoordThe uv texture coordinate at the impact point.
在触碰点的UV纹理坐标。
textureCoord2The secondary uv texture coordinate at the impact point.
在接触点处的第二套UV纹理坐标。
transformThe Transform of the rigidbody or collider that was hit.
碰到的该刚体或碰撞器的变换。
triangleIndexThe index of the triangle that was hit.
碰到的三角形的索引。

 

Returns 返回

bool True when the ray intersects any collider, otherwise false.

当光线投射与任何碰撞器交叉时为真,否则为假。

 

缺省参数:1.layerMask参数缺省为DefaultRaycastLayers,

                    2.distance被缺省为正无穷 ,

                     3. queryTriggerInteraction 被缺省为QueryTriggerInteraction.UseGlobal

                         什么意思呢?我们来看

      

简单来说就是,枚举为Ignore时,所有含碰撞器的物体,都将被射线检测忽略。为UseGlobal时反之。

先来个最简单的Demo

由于我们是以空格键来发射射线,所以Debug.DrawLine(transform.position, hit.transform.position);画得线我截不到。见谅。

******************************************************************************************************************************************************

******************************************************************************************************************************************************

然后是layerMask。我们可以看到默认Layer为Default,我们可以左击选择Add Layer,来查看更详细的层级信息,同时也解释了为什么前面Demo的LayerMask.GetMask()为0。

暂时先写这么多,以后会补充。

 

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中的Physics.Raycast是一个非常重要的函数,它用于检测从一个点发出的一条射线是否与场景中的物体相交。下面详细讲解一下该函数的用法。 语法 Physics.Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal); 参数解释 1. origin:射线的起点,即射线发出的位置。类型为Vector3。 2. direction:射线的方向,即从起点向哪个方向射线。类型为Vector3。 3. maxDistance:射线的最大长度。如果没有物体与射线相交,则该函数返回false。如果相交,则返回true。类型为float。 4. layerMask:射线检测所在的层。类型为int。 5. queryTriggerInteraction:指定是否检测触发器。类型为QueryTriggerInteraction。 返回值 如果射线与场景中的物体相交,则返回true,否则返回false。 使用方法 在Unity中,我们可以通过以下方式来使用Physics.Raycast函数: 1. 在脚本中声明一个射线变量: RaycastHit hit; 2. 调用Physics.Raycast函数,传入起点、方向、最大长度和层级等参数,并将返回值保存在上述定义的射线变量中: if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { // 在这里处理射线与物体相交的情况 } 上述代码中,transform.position表示射线的起点,transform.forward表示射线的方向,out hit表示射线与场景中的物体相交的详细信息,100表示射线的最大长度。 3. 处理射线与物体相交的情况。在上述代码中,我们可以通过hit变量来获取射线与物体相交的详细信息,例如碰撞点、碰撞法线、碰撞物体等信息。 总结 以上就是对Unity函数Physics.Raycast的详细讲解。该函数可以用于玩家射击、角色移动等场景。在使用该函数时,我们需要注意射线的起点、方向、长度、层级等参数,以及如何处理射线与物体相交的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值