字幕列表 影片播放 列印所有字幕 列印翻譯字幕 列印英文字幕 Raycasting is the process of shooting 射線檢查(Raycasting)會從指定座標和軸向 投射一條隱形線並偵測是否有碰撞體在線上 an invisible ray from a point, in a 就像開槍的原理一樣 specified direction to detect whether any 舉例來說, 我們的主角 想要射死那背叛他父親的邪惡方塊 colliders lay in the path of the ray. 射線檢查函式的結構是這樣 One such example of this would be 一開始可能有點難以理解 shooting a gun. In this instance our 但當你明白每個參數所代表的內容 就會變得容易理解 character wants to shoot the evil box that 首先, 射線的原點(origin)是一個世界座標的點 betrayed him and killed his father. 以我們的範例來說 The syntax of the raycast function looks like this. 我們將原點設定在槍口的位置 利用一個三維向量(Vector3)儲存X, Y和Z軸座標 It can be confusing at first 由於世界座標軸向不一定和射擊軸向相同 but once you understand what each part does 因此我們還需要第二個向量值 來儲存我們的射擊軸向(direction) it makes much more sense. 這兩個變數組成了我們的射線 Firstly, the origin of the ray is a 我們也可以帶入一個射線變數(Ray) point in world space. 因為射線變數是一個可以儲存 兩個三維向量的資料類別 So in this instance we'd choose a point in 射線變數用法會長得像這樣 front of the barrel of the gun, stored 下一個參數是射線碰撞參數(RaycastHit) 用來儲存射線碰到的碰撞體資料 as a Vector3, an X, Y and Z position. 有了這個變數程式就可以呼叫 並找到射線路徑上的遊戲物件 However, because our world coordinates 最後還有兩個選擇性參數 direction won't be facing in the direction 第一個是射線距離(Distance) 用來設定射線的長度 we're shooting we will need a second Vector3 如果沒有指定的話預設是無限距離 to store our direction in. 然後是圖層遮罩(Layer Mask) 用來指定射線檢查可忽略的Unity圖層 These two Vector3 variables make up 如果你希望射線忽略某些物件 那你可以指定到圖層遮罩裡 our ray. But we can also substitute 我們在來看一個射線檢查的範例 in a Ray variable, as this data type 這個範例裡我們有一個空投箱 它在接近地面時會打開它的降落傘 can store two Vector3's. 這個空投箱是由兩個部分組成 降落傘和箱子 Our code would then look like this. 降落傘有兩個動畫 The next argument in the function is a 開傘動畫 RaycastHit variable that stores 和收傘動畫 information on the colliders hit. 同時我們需要射線檢查的軸向往下 這樣才知道空投箱與場景地表的距離 So that it can be queried in code as to which 然後透過搜尋environment標籤來計算碰撞體距離 objects are intersected by the ray. 場景的地表已經設定了environment的標籤 Finally there are two optional arguments, 我們就可以在程式腳本裡搜尋這個標籤 Distance, which defines the length 我們將射線函式放在IF判斷式內 所以當傳回True時表示線上有碰撞體 of the ray, if omitted the ray will default 然後就會進入判斷式 並將射線碰撞參數用hit帶出 to an infinite length. 所以判斷式內我們寫入射線的檢查函式 And Layer Mask. This is the number 並設定landingRay來儲存空投箱座標 of a particular layer in Unity's layer system 利用往下的簡寫Vector3.down來指定軸向 on which you can place objects if you 然後將landingRay帶入檢查射線函式 而被帶出射線碰撞參數叫hit wish to make the ray ignore them. 它儲存了射線上碰到的碰撞體 Let's look at another practical example of 我們已設定了deployment height變數 來儲存射線距離 using raycasting. 所以當射線碰到了environment標籤的碰撞體 就會執行開啟降落傘的函式 In this example we have a parachute crate 在此函式裡會把deployed布林值設為True 用來限制這行為只能被執行一次 that opens a parachute when it's 然後我們把物件剛體的線性阻力指到 parachuteEffectiveness這個變數 nearing the floor. 所以當降落傘開啟時 空降箱的移動速度會變慢 The crate is made up of two parts, 然後讓降落傘物件播放開傘動畫 the chute and the crate itself. 開傘動畫就是我們會指定到 全域遊戲物件變數(parachute)的物件 The chute has two animations 然後我們加入一個碰撞進入函式(OnCollisionEnter) one to open the chute 讓空降箱在碰到地面或其他物件時 播放收傘動畫 and another to close it. 我們設定射線距離為4 所以將Deplayment Height設定為4 In this example we need to cast a ray 設定剛體的線性阻力為8 所以將Parachute Effectiveness設定為8 downwards in order to see how far the crate is 然後將降落傘物件拖曳到Parachute設定 from the floor, and we check for the 因為它是附有動畫元件的降落傘物件 這樣我們就可以播放開傘和關傘的動畫 floor by looking for the environment collider. 讓我們在一次執行成果 Our collider for the environment is tagged 有一點值得注意的是 with the word environment. 雖然在遊戲或場景視圖裡你看不到射線 And in our script we are looking for that tag. 但你可以在程式腳本內加入一行 Debug.DrawRay函式來畫出射線 The RayCast function gets placed inside 這樣一來你就可以知道射線的位置與方向 an IF statement so that if it returns true, Debug.DrawRay函式內 我們帶入了空降箱的座標(transform.position) meaning if it intersects with anything, 以及軸向往下的簡寫(Vector3.down) 然後將它乘上射線距離(deploymentHeight) then the comments within the IF statement 這樣一來系統就會畫出和判斷式相同的射線 will be carried out and the RayCastHit 所以, 當我們在播放一次成果時 可以看到場景視圖畫出了一條預覽射線 variable can be queried as to what has been hit. So within an IF statement we've written Physics.Raycast, we have a landingRay variable that's storing the position of the box and a downward direction. We're using the shortcut Vector3.down, and we're using this as the ray to cast. Our RaycastHit variable - 'hit' - is storing anything that gets hit by the ray as it is cast downwards, and the distance, or 'length' or the ray is defined by our 'deployment height' variable. If the ray intersects with a collider then we call the deploy parachute function. This function then simply sets our Boolean 'deployed' flag to true so that this cannot repeat. And then we set the drag of the rigid body to the variable 'parachuteEffectiveness'. So we slow down the crate as if it's being held up by the parachute. We also play the animation on the parachute object, which is a game object that we'll assign to the public variable. We then have a separate OnCollisionEnter function which simply plays the closing animation. So we know that as soon as it hits the ground or another object the parachute can close. So here we've set the length of the ray to 4 by setting 4 as our deployment height And we're setting the drag of the rigidbody to 8 by setting the parachute effectiveness to 8. And we've simply dragged our parachute chute object on to the parachute variable. Because this is the object that has an animation component in order to playback it's opening and closing animations. So let's see that play one more time. It's also worth keeping in mind that although you cannot see raycasts drawn in the scene view or in the game. You can also use the Debug.DrawRay function in order to preview where a ray would be going. By adding Debug.DrawRay we're drawing a visual ray from the position of the box in the direction of Vector3.down, multiplied by the deployment height - the length of our existing ray. And by doing this we've matched the actual ray that we're casting in the IF statement below. So when we play this back you can see that Unity demonstrates the ray by showing us the drawn ray in the scene view.
B1 中級 中文 英國腔 射線 降落傘 變數 物件 動畫 座標 Raycasting - Unity官方教程 (Raycasting - Unity Official Tutorials) 66 3 朱瑛 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字