Unity: Ray射线学习总结

射线 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 变量

direction The direction of the ray.
射线的方向。
origin The origin point of the ray.
射线的原点。

射线的两个基本属性:

origin,射线的起点

direction,射线的方向

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

 

 



猜你喜欢

转载自blog.csdn.net/u013509878/article/details/80044083