Empty
Empty
Empty
CFrame (座標フレーム) はRobloxにおけるデータ型で、オブジェクトの3D空間での位置と方向を表します。CFrameの作成と操作を理解することで、ゲーム内の部品やモデルの配置と回転を精密に制御できます。
位置 | オブジェクトの3D空間内の位置。 |
方向 | オブジェクトの3D空間内の回転。 |
CFrameの例
Empty
Empty
Empty
local myCFrame = CFrame.new(10, 5, -3)
Empty
Empty
Empty
local part = script.Parent
part.Position = Vector3.new(0, 5, 0)
-- Move the part to a new position using CFrame
part.CFrame = CFrame.new(10, 5, -10)
Roblox Studio
Empty
Empty
Empty
local part = script.Parent
part.Position = Vector3.new(0, 0.5, 0)
-- Rotate the part 45 degrees around the Y-axis
part.CFrame = CFrame.new(0, 0.5, 0) * CFrame.Angles(0, math.rad(45), 0)
Roblox Studio
Empty
Empty
Empty
local part = script.Parent
part.Position = Vector3.new(0, 0.5, 0)
-- Move the part to (10, 5, -3) and rotate it 45 degrees around the Y-axis
part.CFrame = CFrame.new(10, 5, -3) * CFrame.Angles(0, math.rad(45), 0)
Roblox Studio
Empty
Empty
Empty
local part = script.Parent
part.Position = Vector3.new(0, 2.5, 0)
-- Make the part look at the point (10, 2.5, -3)
local targetPosition = Vector3.new(10, 2.5, -3)
part.CFrame = CFrame.new(part.Position, targetPosition)
Roblox Studio
CFrame (ワールドスペース対オブジェクトスペース)
ワールドスペース
ワールドスペースはゲーム世界のグローバル座標系を指します。
Empty
Empty
Empty
- ポイントの位置は、世界の固定原点に対して定義されます。
- ポイントの位置は、世界の固定原点に対して定義されます。
- 変換(平行移動、回転、スケーリングなど)は、世界の原点に対して適用されます。
- ワールドスペースの座標は絶対的であり、親や他のオブジェクトの位置や方向の影響を受けません。
オブジェクトスペース
オブジェクトスペースは、オブジェクトに対して相対的な座標系を指します。
Empty
Empty
Empty
- ポイントの位置は、オブジェクトの原点に対して定義されます(世界の原点ではありません)。
- ポイントの位置は、オブジェクトの原点に対して定義されます(世界の原点ではありません)。
- 変換はオブジェクト自体の軸に対して適用されます。これにより、世界の軸と比較して回転およびスケーリングが行われる可能性があります。
- これは、階層内の子オブジェクトを扱う際に特に有用であり、それらの変換は親オブジェクトの位置、方向、およびスケールに基づいています。
Empty
Empty
Empty
-- Get a reference to the parent part
local parentPart = script.Parent
-- Function to create a child part and set its position using local CFrame
local function createChildPart()
-- Create the child part
local childPart = Instance.new("Part")
childPart.Size = Vector3.new(2, 2, 2)
childPart.Anchored = true
childPart.Parent = parentPart
childPart.Name = "Child"
-- Position the child part relative to the parent part using Local CFrame
local partCFrameLocal = CFrame.new(2, 2, 0) -- Local space offset
childPart.CFrame = parentPart.CFrame * partCFrameLocal -- Set the child part's CFrame
print("Child part created with Local Position:", partCFrameLocal.Position)
-- Print parent's world space position
print("Parent Part's World Position:", parentPart.CFrame.Position)
-- Demonstrating ToWorldSpace and ToObjectSpace
local modelCFrameWorld = parentPart.CFrame -- Parent's world CFrame
local partCFrameWorld = modelCFrameWorld:ToWorldSpace(partCFrameLocal)
print("World Position:", partCFrameWorld.Position) -- Prints the position of the part in world space
local recalculatedPartCFrameLocal = modelCFrameWorld:ToObjectSpace(partCFrameWorld)
print("Recalculated Local Position:", recalculatedPartCFrameLocal.Position) -- Should match partCFrameLocal.Position
end
-- Call the function to create the child part
createChildPart()
Roblox Studio