Unity: Ray射線學習總結
阿新 • • 發佈:2019-01-06
Returns a point at distance units along the ray. 返回射線上指定距離的點。 |
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):
1public 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)判斷物體前方是否有物體
123456789101112voidUpdate(){RaycastHithit;if(Physics.Raycast(transform.position,-Vector3.up,outhit))floatdistanceToGround=hit.distance;}
(2) 引數為射線時,
123456789voidUpdate(){Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);if(Physics.Raycast(ray,100))print("Hitsomething");}(3)返回碰撞資訊
1234567891011121314voidUpdate(){Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHithit;if(Physics.Raycast(ray,outhit,100))Debug.DrawLine(ray.origin,hit.point);}