Unity射线检测Physics.Raycast

Unity射线检测Physics.Raycast

常用方法:

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

Parameters 参数

origin The starting point of the ray in world coordinates.在世界坐标,射线的起始点。
direction The direction of the ray.射线的方向。
distance The length of the ray.射线的长度。
layermask A Layer mask that is used to selectively ignore colliders when casting a ray.投射射线,选择投射的层蒙版。
queryTriggerInteraction Specifies whether this query should hit Triggers.指定是否查询碰到触发器。
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).hitInfo将包含碰到器碰撞的更多信息。
ray The 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 变量

barycentricCoordinate The barycentric coordinate of the triangle we hit.碰到的三角形的重心坐标。
collider The Collider that was hit.碰到的碰撞器。
distance The distance from the ray’s origin to the impact point.从射线的原点到触碰点的距离。
lightmapCoord The uv lightmap coordinate at the impact point.在触碰点的UV光照贴图的坐标。
normal The normal of the surface the ray hit.射线触碰表面的法线。
point The impact point in world space where the ray hit the collider.在世界坐标空间,射线碰到碰撞器的接触点。
rigidbody The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.碰到的该碰撞器上的刚体。如果碰撞器上没有附加刚体,那么返回null。
textureCoord The uv texture coordinate at the impact point.在触碰点的UV纹理坐标。
textureCoord2 The secondary uv texture coordinate at the impact point.在接触点处的第二套UV纹理坐标。
transform The Transform of the rigidbody or collider that was hit.碰到的该刚体或碰撞器的变换。
triangleIndex The 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默认的)

Unity射线检测Physics.Raycast_第1张图片
代码:

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);
		}
	}

你可能感兴趣的:(Unity.Raycast)