ノックバック
Coddyの「Robloxゲーム:バトルグラウンド」コースのレッスン 11/25。
叩きつけると、当たったものを吹き飛ばします。ルートに対して ApplyImpulse を使うと、全身に力が加わります。インパルスは質量に速度変化を掛けたものなので、AssemblyMass を掛けると、軽いリグも重いリグも同じように吹き飛ばせます。
theirRoot:ApplyImpulse((away * 30 + Vector3.new(0, 20, 0)) * theirRoot.AssemblyMass)away は、地面に沿ってあなたから相手へ向かう方向を示します。相手の位置からあなたの位置を引き、Yを0にしてから、長さが1になるように .Unit を適用します。
チャレンジ
中級スラムが当たった全員を吹き飛ばすようにしましょう。
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)を追加します。
「プレイ」を押し、2体の赤いリグの間を歩いてEを押します。2体が吹き飛びます。その後、「確認」を押します。
自分で試してみよう
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 = {} -- 剣を振るたびに、各humanoidを1回だけ処理する
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プレイグラウンド