RayValue

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

一个 RayValue 是一个用于存储单个射线的目标对象。与 CFrameValue 类似,在工作室内的属性窗口中无法查看或编辑 RayValue 的存储射线。而是使用命令栏获取和设置这些对象的值。例如,你可以使用像下面这样的一行来在 Workspace 中创建一个名为“Value”的新 RayValue。它在 (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