Vector3Value

顯示已棄用項目

*此內容是使用 AI(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)