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 被更改時發射。
屬性
方法
活動
Changed
每當 Vector3Value.Value 的 Vector3Value 被更改時,都會發射。它將使用新值存儲在參數對物件中,而不是代表正在變更的屬性的字串。
這個事件,像其他已變更的事件一樣,可以用來跟蹤當 Vector3Value 變更時以及可能變更的不同值。
例個體、實例,這可能在使用 Vector3Values 追蹤遊戲世界位置的遊戲中有用。
相同類型的變更事件存在於類似物件,例如 NumberValue 和 StringValue,取決於哪種物件類型最適合需求。
參數
變更後的新值。
範例程式碼
以下示例,假設所有參考對象存在,每次變更時都會列印 Vector3Value 的新值。在下面的例子中,將印出 "10,10,10" 。
如何使用 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)