【unity2D】API-學習記錄9-射線類Ray2D


目標

在游戲開發中,射線的使用非常廣泛。今天來學習Ray2D的相關內容以及如何發射2D射線


前言

  1. unity中有應用於2D游戲中的射線,其中,Ray2D射線類和RaycastHit2D射線投射碰撞信息類是最常用的兩個射線工具類。前者用來創建射線,后者用來存儲發射射線后產生的碰撞信息。今天先來學習射線類Ray2D。
  2. UnityAPI中文文檔看上去像是機翻,有些描述很莫名其妙,我這里對其中一些描述進行了改動,改動處會標明有“(改動前)”和“(改動后)”字樣

Ray2D是什么

Ray2D是一個類,下圖展示了它的構造函數:

image

變量 解釋
origin 世界空間中的射線起點。
direction 世界空間中的射線方向。
API對Ray2D的解釋
Ray2D

A ray in 2D space.
2D 空間中的射線。

A ray is a line segment that extends from a point in space in a specified direction. 
射線 是從空間中的某個點開始,按照指定方向延伸的線段。

Rays have a number of uses in Unity but the most common is probably raycasting. 
射線在 Unity 中具有多種用途,但是最常用的可能是射線投射。

This technique involves tracing along the path of a ray from its origin to determine if it intersects with any objects.
此方法涉及沿着射線路徑從其原點進行追蹤,以確定它是否與任何對象相交。

This is useful for plotting the paths of projectiles, determining lines of sight and implementing many common game mechanics.
這可用於繪制飛彈的路徑、確定視線以及實現許多常見的游戲機制。

公共函數

公共函數 解釋
GetPoint 獲取射線上位於給定距離處的點。

如何發射2D射線

發射2D射線用到的是:Physics2D.Raycast()
image

API對Physics2D.Raycast的解釋


Physics2D.Raycast

public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, 
int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
參數 解釋
origin 射線在 2D 空間中的起點。
direction 表示射線方向的矢量。
distance 射線的最大投射距離。
layerMask 過濾器,用於僅在特定層上檢測碰撞體。
minDepth 僅包括 Z 坐標(深度)大於或等於該值的對象。
maxDepth 僅包括 Z 坐標(深度)小於或等於該值的對象。

Returns
返回

RaycastHit2D The cast results returned.
RaycastHit2D 返回的投射數量。//(改動前)
返回RaycastHit2D類型的投射結果。//(改動后)

Description
描述

Casts a ray against Colliders in the Scene.
向場景中的碰撞體投射射線。

A raycast is conceptually like a laser beam that is fired from a point in space along a particular direction.
從概念上說,射線投射 類似於從空間中的某個點朝特定方向發射一條光束。

Any object making contact with the beam can be detected and reported.
在該過程中,可以檢測並報告與光束接觸的任何對象。


This function returns a RaycastHit object with a reference to the Collider that is hit by the ray
(the Collider property of the result will be NULL if nothing was hit). 
函數返回一個 RaycastHit 對象,該對象引用了射線命中的碰撞(如果未命中任何對象,則結果的碰撞體屬性將為 NULL)。

The layerMask can be used to detect objects selectively only on certain layers
(this allows you to apply the detection only to enemy characters, for example).
LayerMask 可用於僅在特定層上有選擇地檢測對象(例如,這讓您能夠僅將檢測應用於敵人角色)。


Overloads of this method that use contactFilter can filter the results by the options available in ContactFilter2D.
使用 contactFilter 的此方法重載可以按 ContactFilter2D 中提供的選項篩選結果。


Raycasts are useful for determining lines of sight, targets hit by gunfire and for many other purposes in gameplay.
對於確定視線、炮火擊中的目標以及游戲中的許多其他目的來說,射線投射很有用。

Additionally, this will also detect Collider(s) at the start of the ray. 
此外,這還將檢測位於射線起點的碰撞體。

In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. 
在這種情況下,射線從碰撞體內部開始,並且不與碰撞體表面交疊。

This means that the collision normal cannot be calculated, 
in which case the returned collision normal is set to the inverse of the ray vector being tested.
這意味着無法計算碰撞法線,在這種情況下,返回的碰撞法線設置為正在測試的射線向量的倒數。

This can easily be detected because such results are always at a RaycastHit2D fraction of zero.
這可輕松檢測到,因為此類結果始終是 RaycastHit2D 分數為零。

最后一段有些難懂,具體請參考RaycastHit2D.normalRaycastHit2D.fraction


注意事項

經過測試發現:

  1. 對於“尚未設置distance或distance設置過短”的Physics2D.Raycast,如果射線起點即在碰撞體內,則檢測到該碰撞體並返回相關的RaycastHit2D,不再向前檢測了。
  2. 對於“設置了足夠長的distance”的Physics2D.Raycast,它會返回檢測到的最后一個碰撞器的相關RaycastHit2D
  3. 射線可以檢測到觸發器

補充

  1. 關於Physics2D.Raycast,其可以返回RaycastHit2D類型的結果,關於RaycastHit2D的內容之后再學(其實不能把它們割裂開理解)
  2. Physics2D.Raycast還可以返回int類型的結果,具體有何作用,請參考Unity API Physics2D.Raycast

參考資料

Unity API Ray2D
Unity API Physics2D.Raycast


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM