RayValue

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

一個 RayValue 是一個僅用於儲存單個光線的對象。與 CFrameValue 類似,在工作室內的屬性窗口中無法查看或編輯 RayValue 的儲存射線。取而代之,使用指令欄來獲得和設置這些對象的值。例如,您可以使用以下類似線來在 Workspace 中創建新的 RayValue 名為 "Value" 的值。它在(0, 50, 0)創建一個射線,並面向正向X方向。

Instance.new("RayValue").Value = Ray.new(Vector3.new(0, 50, 0), Vector3.new(10, 0, 0))

因為 Studio 內沒有簡單的方法來編輯射線,所以有時候最好使用 CFrameValue (可以通過零件或相攝影機變更)。您可以使用 Ray.new(cf.p, cf.lookVector * dist) 從 CFrame 重建一個射線,其中 cf 是指定的 CFrame,而 dist 是要建建構的射線長度。

像所有 "-Value" 對象一樣,這個單值存儲在價值屬性中。對於這個 (以及其他像它一樣的對象) 所做的更改事件將使用新值存儲在對物件中,而不是使用代表被更改的屬性的字串。

範例程式碼

This code sample demonstrates constructing a Ray, storing the Ray within a RayValue and Raycasting to find other parts between two parts named "PartA" and "PartB".

Rays, RayValue and Raycasting

local partA = workspace.PartA
local partB = workspace.PartB
local origin = partA.Position
local direction = partB.Position - partA.Position
local ray = Ray.new(origin, direction)
local rayValue = Instance.new("RayValue")
rayValue.Value = ray
rayValue.Name = "PartA-to-PartB Ray"
rayValue.Parent = workspace
-- Raycast to find any parts in between PartA and PartB
local part = workspace:FindPartOnRay(ray)
if part then
print("Hit part: " .. part:GetFullName())
else
-- This ought to never happen, as our ray starts at PartA
-- and points towards PartB, so we should always hit PartB
-- or some other part.
print("No part hit!")
end

屬性

Value

平行讀取

已儲存的射線。

方法

活動

Changed

此事件會在 RayValue.Value 屬性發生變更時發射。它將使用新值存儲在參數對物件中,而不是代表正在變更的屬性的字串。

這個事件,像其他已變更的事件一樣,可以用來跟蹤當 RayValue 變更時以及可能變更的不同值。

相同類型的變更事件存在於類似物件,例如 NumberValueStringValue,取決於哪種物件類型最適合需求。

參數

value: Ray

變更後的值。


範例程式碼

The below example, assuming all referenced objects existed, would print the RayValue's new value each time it changed. In the example below it would print "{0, 0, 0}, {0.577350199, 0.577350199, 0.577350199}".

How to Use RayValue.Changed

local value = Instance.new("RayValue")
value.Parent = workspace
value.Changed:Connect(function(NewValue)
print(NewValue)
end)
local start = Vector3.new(0, 0, 0)
local lookAt = Vector3.new(10, 10, 10)
local ray = Ray.new(start, (lookAt - start).Unit)
value.Value = ray