히트박스
Coddy의 Roblox 게임: 배틀그라운드 코스 레슨. 25개 중 3번째.
휘두른 검은 무언가에 맞아야 합니다. "내 앞에 무엇이 있지?"는 공간 쿼리입니다. workspace:GetPartBoundsInBox는 상자(중심을 나타내는 CFrame과 크기를 나타내는 Vector3)를 받아 그 상자와 겹치는 모든 파트를 반환합니다.
local box = root.CFrame * CFrame.new(0, 0, -3) -- 앞쪽으로 3 studs
local parts = workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params)파트의 모델에는 Humanoid가 있으므로, part.Parent:FindFirstChildOfClass("Humanoid")를 사용하면 그 파트가 피해를 입을 수 있는 대상에게 속하는지 알 수 있습니다.
자신의 팔도 그 상자 안에 들어갑니다. OverlapParams로 쿼리를 필터링하세요. 캐릭터를 FilterDescendantsInstances에 넣으면 상자에 자신의 신체가 검색되지 않습니다.
챌린지
쉬움네 앞에 서 있는 대상이 휘두르기에 맞으면 피해를 입게 하세요.
swing의 끝에서FilterDescendantsInstances = { character }를 사용해OverlapParams를 만드세요.root.CFrame * CFrame.new(0, 0, -3)의GetPartBoundsInBox에서 각 파트를 가져오고 크기는Settings.HitboxSize로 지정하세요. 해당 파트의 부모에 Humanoid가 있으면TakeDamage(Settings.SwordDamage)를 호출하세요.- 그런 다음
print(humanoid.Parent.Name .. " is at " .. humanoid.Health)를 실행하세요.
‘플레이’를 누르고 훈련용 인형을 공격하세요. Output에 체력이 줄어드는 것이 표시됩니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local lastSwing = {} -- player -> 마지막으로 휘두른 시각
-- Roblox 자체 검 애니메이션
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"
print("Swords deal " .. Settings.SwordDamage .. " damage")
-- 검 휘두르기: SwingCooldown마다 최대 한 번 애니메이션 재생
local function swing(player)
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if not root or character.Humanoid.Health <= 0 then
return
end
local now = time()
if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
return
end
lastSwing[player] = now
character.Humanoid.Animator:LoadAnimation(slash):Play()
-- 레슨 3: 여기에 히트박스를 추가합니다
end
-- 모든 플레이어는 부활할 때마다 검을 받습니다
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local sword = ServerStorage.Sword:Clone()
sword.Parent = player.Backpack
sword.Activated:Connect(function()
swing(player)
end)
end)
end)
Roblox 게임: 배틀그라운드의 모든 레슨
직접 연습해 보세요: Roblox 플레이그라운드