CFrame

CFrame (좌표 프레임)은 Roblox에서 객체의 위치와 방향을 나타내는 데이터 유형입니다. CFrame을 생성하고 조작하는 방법을 이해하면 게임 내 부품과 모델의 위치와 회전을 정확하게 제어할 수 있습니다.

위치객체의 3D 세계 내 위치.
방향객체의 3D 세계 내 회전.

CFrame 예제

local myCFrame = CFrame.new(10, 5, -3)
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)
Image 1
Roblox Studio
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)
Image 1
Roblox Studio
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)
Image 1
Roblox Studio
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)
Image 1
Roblox Studio

CFrame (월드 공간 vs 객체 공간)

월드 공간

WorldSpace는 게임 세계의 글로벌 좌표 시스템을 나타냅니다.
  • 점의 위치는 고정된 세계의 원점에 상대적으로 정의됩니다.
  • 점의 위치는 고정된 세계의 원점에 상대적으로 정의됩니다.
  • 변환 (이동, 회전 및 크기 조정)은 세계의 원점에 상대적으로 적용됩니다.
  • WorldSpace에서의 좌표는 절대적이며, 부모 객체 또는 다른 객체의 위치나 방향에 영향을 받지 않습니다.

객체 공간

ObjectSpace는 객체에 상대적인 좌표 시스템을 나타내며, 로컬 공간이라고도 합니다.
  • 점의 위치는 객체의 원점에 상대적으로 정의됩니다.
  • 점의 위치는 객체의 원점에 상대적으로 정의됩니다.
  • 변환은 객체 자체 축을 기준으로 적용되며, 이는 세계 축과 비교했을 때 회전 및 크기 조정이 가능합니다.
  • 이것은 특히 계층 구조 내의 자식 객체를 다룰 때 유용합니다. 그들의 변환은 부모 객체의 위치, 방향 및 크기 조정에 기반합니다.
-- 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()
Image 1
Roblox Studio

이 튜토리얼이 도움이 되셨다면, 저의 작업을 지원하기 위해 커피 한 잔을 사주시면 감사하겠습니다.

지원해 주셔서 정말 감사합니다!

커피 한 잔 사주기