[모듈 공유] 타워 게임 - Moving 파트 만들기

beans·2023년 5월 20일
0

1. ServerScriptServiceScript 추가

2. Script 이름 MovingBlockScript로 변경

3. MovingBlockScript에 아래 코드 붙여넣기




-- Script by bean7189



-- 이 스크립트는 Block-Moving 파트가 Start 파트의 위치에서 시작하여 End 파트의 위치에 도달한 후 다시 Start 파트 위치로 이동을 반복하는 기능을 수행하는 스크립트 입니다.
-- Block-Moving 파트 내에는 움직이기 시작할 위치용 파트인 Start 파트와 목표 위치용 파트인 End 파트가 존재해야 합니다.



--// Functions //--

-- Workspace 내에 있는 모든 Part를 찾아 이름에 Damage가 있을 경우 데미지를 줄 수 있도록 작성된 함수.
local function InitializeMovingBlocks()
    for _, block: BasePart in pairs(workspace:GetDescendants()) do
        if not block:IsA('BasePart') then continue end
        if not string.find(block.Name, 'Moving') then continue end

        local vSpeed: NumberValue = block:FindFirstChild('Speed')
        local speed = (vSpeed and vSpeed.Value) and vSpeed.Value or 15
        local moveStart: BasePart = block:FindFirstChild('Start')
        if not moveStart then warn('[Server] Moving 블록에 Start 파트가 존재해야 합니다: ' .. tostring(block:GetFullName())) return end
        local moveEnd: BasePart = block:FindFirstChild('End')
        if not moveEnd then warn('[Server] Moving 블록에 End 파트가 존재해야 합니다: ' .. tostring(block:GetFullName())) return end

        moveStart.Transparency = 1
        moveEnd.Transparency = 1

        local Attachment0 = Instance.new('Attachment')
        Attachment0.Parent = moveEnd
        Attachment0.Name = 'Attachment0'
        Attachment0.WorldCFrame = moveEnd.CFrame
        local Attachment1 = Instance.new('Attachment')
        Attachment1.Parent = block
        Attachment1.Name = 'Attachment1'
        Attachment1.WorldCFrame = block.CFrame
        local PrismaticConstraint = Instance.new('PrismaticConstraint')
        PrismaticConstraint.Parent = block
        PrismaticConstraint.Attachment0 = Attachment0
        PrismaticConstraint.Attachment1 = Attachment1
        PrismaticConstraint.ActuatorType = Enum.ActuatorType.Servo
        PrismaticConstraint.ServoMaxForce = 10000
        PrismaticConstraint.Speed = speed

        task.spawn(function()
            block.Position = moveStart.Position
            local originMagnatitude = (block.Position - moveEnd.Position).Magnitude

            while true do
                PrismaticConstraint.TargetPosition = moveEnd.Position.Magnitude
                repeat task.wait() until math.floor((block.Position - moveEnd.Position).Magnitude) == 0
                PrismaticConstraint.TargetPosition = -originMagnatitude
                repeat task.wait() until math.floor((block.Position - moveStart.Position).Magnitude) == 0
            end
        end)
    end
end

----



--// Main //--
-- Workspace 내 모든 데미지 블록에 기능 부여
InitializeMovingBlocks()
----

4. 움직이는 Moving 파트 만들기

  • Block-Moving: 직접적으로 움직이게될 파트. 이 파트 위에 올려져있는 모든 Anchor 되어있지 않은 파트는 Block-Moving 파트가 움직일 때 같이 움직일 수 있음
    (예: 캐릭터가 Block-Moving 파트 위에 있으면 캐릭터가 운반됨)
  • Start, End: Block-Moving 파트가 Start 파트의 위치에서 End 파트까지 움직임. End 파트에 도달할 경우 Start 파트로 되돌아감. 이후 Start 파트에 도달할 경우 다시 End 파트로 이동을 반복

5. 완성 모습 (동영상)

0개의 댓글