每次挥剑一次
Coddy Roblox 游戏:竞技场对战 课程的第 4 课,共 25 课。
一次挥击打印了三行,并让假人减少了 30 点生命值。它的根部、腿和头是三个部位,都在碰撞箱内,而且每个部位都击中了同一个 Humanoid。
用表就能解决这个问题:记住这次挥击击中过的每个 Humanoid,并跳过已经见过的目标。表的键可以是实例:
local hit = {}
if humanoid and not hit[humanoid] then
hit[humanoid] = true
-- 对它造成一次伤害
end这个表是在 swing 内创建的,因此每次挥击都会从一个空表开始,而且碰撞箱内的两个敌人都会各被击中一次。
挑战
简单每次挥剑时,每个敌人只受击一次。
- 在
swing中,在循环之前添加local hit = {}。 - 只有在满足
if humanoid and not hit[humanoid]时才对 Humanoid 造成伤害,并在造成伤害时设置hit[humanoid] = true。
按“开始”并攻击训练假人:输出中会显示 Dummy is at 90,且只显示一次。然后按“检查”。
亲手试试
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")
-- 挥剑:先播放动画,再击中你前方判定区域内的所有人
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()
-- 判定区域在你前方,不包括你自己的身体
local params = OverlapParams.new()
params.FilterDescendantsInstances = { character }
local box = root.CFrame * CFrame.new(0, 0, -3)
for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
-- 第 4 课:在这里添加每次挥剑只命中一次的逻辑
if humanoid then
humanoid:TakeDamage(Settings.SwordDamage)
print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
end
end
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 在线编辑器