Vector3Value 简单地保存一个 Vector3 作为值。这个值可用于脚本通信、对象移至预设位置等。
代码示例
这段代码示例会导致零件传送任何触碰它的玩家到特定位置定义的“传送位置”Vector3Value。
传送器部分
-- 将我粘贴到零件内的脚本中
local part = script.Parent
local teleportPosition = part.TeleportPosition
local function onTouch(otherPart)
-- 首先,找到 HumanoidRootPart。如果我们找不到它,退出。
local hrp = otherPart.Parent:FindFirstChild("HumanoidRootPart")
if not hrp then
return
end
-- 现在通过设置 CFrame 到从中创建的一个进行传送
-- 存储的传送位置
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)
概要
活动
每当 Vector3Value.Value 被更改时发射。
属性
Value
方法
活动
Changed
参数
代码示例
如何使用 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)