Vector3Value

显示已弃用

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

Vector3Value 简单地保存一个 Vector3 作为值。这个值可用于脚本通信、对象移至预设位置等。

代码示例

This code sample causes a Part to teleport any players that touch it to a specific position defined by a "TeleportPosition" Vector3Value.

Teleporter Part

-- Paste me in a Script inside a Part
local part = script.Parent
local teleportPosition = part.TeleportPosition
local function onTouch(otherPart)
-- First, find the HumanoidRootPart. If we can't find it, exit.
local hrp = otherPart.Parent:FindFirstChild("HumanoidRootPart")
if not hrp then
return
end
-- Now teleport by setting the CFrame to one created from
-- the stored TeleportPosition
hrp.CFrame = CFrame.new(teleportPosition.Value)
end
part.Touched:Connect(onTouch)

This code sample demonstrates how it is possible to store a Vector2 within a Vector3Value by converting a Vector2 into a Vector3 with a dummy Z value. Similarly, you can load it by reconstructing the Vector2 from the X and Y axes.

Storing Vector2 inside Vector3Value

local vector3Value = Instance.new("Vector3Value")
-- Store a Vector2 in a Vector3
local vector2 = Vector2.new(42, 70)
vector3Value.Value = Vector3.new(vector2.X, vector2.Y, 0) -- The Z value is ignored
-- Load a Vector2 from a Vector3
vector2 = Vector2.new(vector3Value.Value.X, vector3Value.Value.Y)
print(vector2)

属性

Value

读取并联

存储的 Vector3

方法

活动

Changed

每当 Vector3Value.ValueVector3Value 被更改时,都会发射。它将运行在新值被存储在参数对象中,而不是代表正在更改的属性的字符串。

这个事件,像其他更改的事件一样,可以用来跟踪当 Vector3Value 发生变更时以及可能更改的不同值。

例实例,这可能对那些依赖 Vector3Values 来跟踪游戏世界位置的游戏有用。

相同类型的更改事件存在于类似对象,例如 NumberValueStringValue,根据需求选择最适合的对象类型。

参数

value: Vector3

更改后的新值。


代码示例

The below example, assuming all referenced objects existed, would print the Vector3Value's new value each time it changed. In the example below it would print "10,10,10".

How to Use Vector3Value.Changed

local value = Instance.new("Vector3Value")
value.Parent = workspace
value.Changed:Connect(function(NewValue)
print(NewValue)
end)
value.Value = Vector3.new(10, 10, 10)