按 Q 冲刺
Coddy Roblox 游戏:竞技场对战 课程的第 7 课,共 25 课。
冲刺就是一次推力:设置根部的 AssemblyLinearVelocity 会把角色弹出去。但 Roblox 允许每个玩家的设备运行自己角色的物理模拟,因此服务器设置的推力几乎无法推动他们。由服务器做决定,再由玩家自己的 LocalScript 推动角色。
服务器按名称将能力保存在一个表中,同意冲刺后,会通过 FireClient 将消息发送回该玩家的客户端,并由那里的 OnClientEvent 接收:
remotes.Ability.OnClientEvent:Connect(function(name)
if name == "Hop" then
root.AssemblyLinearVelocity = Vector3.new(0, 50, 0)
end
end)挑战
中等实现按 Q 冲刺:服务器同意后,你的设备负责推动角色。
- 在
BattleServer中,用一个abilities表替换 print:abilities.Dash(player)调用remotes.Ability:FireClient(player, "Dash")。 - 如果处理程序存在,就运行
abilities[name](player, character, root)。 - 在
BattleClient中,当remotes.Ability.OnClientEvent收到"Dash"时,将根部件的速度设为root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0)。
按“运行”,再按 Q:你会向前冲刺。然后点击“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- 玩家 -> 上次挥剑的时间
-- 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)
local hit = {} -- 每次挥剑,每个 humanoid 只命中一次
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
humanoid:TakeDamage(Settings.SwordDamage)
print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
end
end
end
-- 客户端通过名称请求使用技能
remotes.Ability.OnServerEvent:Connect(function(player, name)
print(player.Name .. " asked for " .. name)
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 在线编辑器