射线: Ray是具有开始点和方向的无穷线。
射线Ray 通常会与 物理属性 Physics.Raycast 射线投射一起用;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Rayray;//定义射线
privatevoidAwake()
{
ray=newRay();//初始化
}
privatevoidUpdate()
{
//设置射线的初始位置及方向
ray.origin=transform.position;
ray.direction=transform.forward*10f;
//射线碰撞检测
RaycastHitraycastHit;
if(Physics.Raycast(ray,outraycastHit))//检测是否有射线碰撞,有的话返回碰撞信息
{
//画线,参数为:起点,终点,颜色
Debug.DrawLine(transform.position,raycastHit.point,Color.red);
//raycastHit包含了碰撞信息
if(raycastHit.transform.name=="B")//判断射线检测到的物体是否为"B"
{
Destroy(raycastHit.transform.gameObject);//销毁被射线扫描到的游戏对象
}
}
}
Variables 变量
Constructors 构造器
Ray | Creates a ray starting at origin along direction. 创建一条射线从origin开始,沿direction方向。 |
声明一条射线:
1
Ray
ray
;
Public Functions 共有函数
GetPoint | Returns a point at distance units along the ray. 返回射线上指定距离的点。 |
ToString | Returns a nicely formatted string for this ray. 返回该射线格式化好的字符串。 |
Physics.Raycast 射线投射
重载(1):
1 ·public static bool Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);··· XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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. 指定是否查询碰到触发器。 |
Returns 返回是否与其他碰撞器碰撞;
重载(2):
1 public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);··· XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Parameters 参数
origin | The starting point of the ray in world coordinates. 在世界坐标,射线的起始点。 |
direction | The direction of the ray. 射线的方向 |
hitInfo | If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). hitInfo将包含碰到器碰撞的更多信息。 |
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. 指定是否查询碰到触发器。 |
Returns 返回是否与其他碰撞器碰撞;
重载(3):
1 public static bool Raycast(Ray ray, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);··· XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Parameters 参数
ray | The starting point and 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. 指定是否查询碰到触发器。 |
Returns 返回是否与其他碰撞器碰撞;
重载(4):
1
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask =
DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
Parameters 参数
ray | The starting point and direction of the ray. 射线的起点和方向 。 |
hitInfo | If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). hitInfo将包含碰到器碰撞的更多信息。 |
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. 指定是否查询碰到触发器。 |
Returns 返回是否与其他碰撞器碰撞;
四个重载其实都差不太多,都包含射线,碰撞信息等
常用功能案例:
(1)判断物体前方是否有物体
1
2
3
4
5
6
7
8
9
10
11
12
void
Update
(
)
{
RaycastHit
hit
;
if
(
Physics
.
Raycast
(
transform
.
position
,
-
Vector3
.
up
,
out
hit
))
float
distanceToGround
=
hit
.
distance
;
}
(2) 参数为射线时,
1
2
3
4
5
6
7
8
9
voidUpdate(){
Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,100))
print("Hitsomething");
}
(3)返回碰撞信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void
Update
(
)
{
Ray
ray
=
Camera
.
main
.
ScreenPointToRay
(
Input
.
mousePosition
)
;
RaycastHit
hit
;
if
(
Physics
.
Raycast
(
ray
,
out
hit
,
100
))
Debug
.
DrawLine
(
ray
.
origin
,
hit
.
point
)
;
}