Unity射线检测Physics.Raycast

Unity射线检测Physics.Raycast

常用方法:

public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask);

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.投射射线,选择投射的层蒙版。
queryTriggerInteractionSpecifies whether this query should hit Triggers.指定是否查询碰到触发器。
hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).hitInfo将包含碰到器碰撞的更多信息。
rayThe starting point and direction of the ray.射线的起点和方向

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.碰到的三角形的索引。

缺省参数
1.layerMask参数缺省为DefaultRaycastLayers,
2.distance被缺省为正无穷 ,
3. queryTriggerInteraction 被缺省为QueryTriggerInteraction.UseGlobal

Ignore时,所有含碰撞器的物体,都将被射线检测忽略。为UseGlobal时反之。

int layerMask = 1<<8 表示投射第8层上检查碰撞信息;(0<<8 )则表示忽略第8层的碰撞

~layerMask 投射8层之外检查碰撞信息(排除第八层);

~(1<<0) 投射所有层;

(1 << 9) | (1 << 8) 投射第9和第8的层。

在物体的layer中,可以选择ignoreRayCast,则不投射该层。

如果不传入layermask这个值的话,那么默认的会值忽略 IgnoreRaycast 这个层级(unity默认的)

在这里插入图片描述
代码:

void Update () 
	{
        Vector3 down = transform.TransformDirection(Vector3.back);
        RaycastHit hit;
        if(Physics.Raycast(transform.position,down,out hit,1))
        {
            Debug.DrawLine(transform.position, hit.point, Color.red);
		}
	}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值