击退
Coddy Roblox 游戏:竞技场对战 课程的第 11 课,共 25 课。
猛击应该把被击中的目标击飞。对根部件调用 ApplyImpulse,就能推动整个身体。冲量等于速度变化乘以质量,所以乘以 AssemblyMass 后,无论装配体轻重,击飞效果都一样:
theirRoot:ApplyImpulse((away * 30 + Vector3.new(0, 20, 0)) * theirRoot.AssemblyMass)away 指向从你到对方的方向,并且贴着地面:用对方的位置减去你的位置,将 Y 设为 0,然后用 .Unit 将长度设为 1。
挑战
中等让猛击把它击中的所有人都击飞。
- 在
abilities.Slam中,hurt之后添加:local away = (theirRoot.Position - root.Position) * Vector3.new(1, 0, 1),然后如果它的 Magnitude 大于 0,就添加away = away.Unit。 - 然后添加
theirRoot:ApplyImpulse((away * Settings.SlamKnockback + Vector3.new(0, 40, 0)) * theirRoot.AssemblyMass)。
按“开始”,在两个红色人偶之间走动,然后按 E:它们会飞向两边。然后点击“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> 上次挥剑的时间
-- Roblox 自带的剑术动画
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"
print("Swords deal " .. Settings.SwordDamage .. " damage")
-- 游戏中的每次命中都在这里处理
local function hurt(humanoid, amount)
if humanoid.Health <= 0 then
return
end
local character = humanoid.Parent
humanoid:TakeDamage(amount)
end
-- 挥剑:先播放动画,再击中你面前判定框内的所有人
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)
local hit = {} -- 每次挥剑,每个人形角色只命中一次
for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
if humanoid and not hit[humanoid] then
hit[humanoid] = true
hurt(humanoid, Settings.SwordDamage)
end
end
end
-- 按名称列出各个技能及其作用
local abilities = {}
-- 玩家自己的设备负责移动角色,所以冲刺也在那里执行
function abilities.Dash(player)
remotes.Ability:FireClient(player, "Dash")
end
-- 击中你周围的所有人
function abilities.Slam(player, character, root)
local params = OverlapParams.new()
params.FilterDescendantsInstances = { character }
local hit = {}
for _, part in workspace:GetPartBoundsInRadius(root.Position, Settings.SlamRadius, params) do
local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
local theirRoot = part.Parent:FindFirstChild("HumanoidRootPart")
if humanoid and theirRoot and not hit[humanoid] then
hit[humanoid] = true
hurt(humanoid, Settings.SlamDamage)
-- 第 11 课:在这里添加击退效果
end
end
end
-- 客户端按名称请求使用技能。服务器会检查:技能是否存在、
-- 玩家是否存活,以及冷却时间是否结束。技能再次可用的时间
-- 是玩家的一个属性,这样就能显示在玩家的屏幕上。
remotes.Ability.OnServerEvent:Connect(function(player, name)
if typeof(name) ~= "string" or not abilities[name] then
return
end
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if not root or character.Humanoid.Health <= 0 then
return
end
local now = workspace:GetServerTimeNow()
if now < (player:GetAttribute(name .. "ReadyAt") or 0) then
return
end
player:SetAttribute(name .. "ReadyAt", now + Settings.Cooldowns[name])
abilities[name](player, character, root)
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)
-- 训练假人:血条会随生命值变化。被击败后,
-- 假人过一会儿会在原地重新出现,成为一个全新的副本
local function setupDummy(dummy)
local humanoid = dummy.Humanoid
humanoid.HealthChanged:Connect(function(health)
dummy.HealthBar.Back.Fill.Size = UDim2.fromScale(health / humanoid.MaxHealth, 1)
end)
humanoid.Died:Connect(function()
local spot = dummy:GetPivot()
task.wait(0.5)
dummy:Destroy()
task.wait(Settings.DummyRespawn)
local fresh = ServerStorage.Dummy:Clone()
fresh:PivotTo(spot)
fresh.Parent = workspace
setupDummy(fresh)
end)
end
setupDummy(workspace.Dummy)
Roblox 游戏:竞技场对战中的所有课
自己练习: Roblox 在线编辑器