地面叩きつけ
Coddyの「Robloxゲーム:バトルグラウンド」コースのレッスン 10/25。
2つ目のアビリティはEキーで発動します。周囲の全員に当たる地面叩きつけです。workspace:GetPartBoundsInRadius(position, radius, params)は、振りの当たり判定ボックスと同じように、円形の範囲内にあるパーツを見つけます。
for _, part in workspace:GetPartBoundsInRadius(root.Position, 6, params) doこれでゲームには相手にダメージを与える方法が2つあり、さらに増えていきます。すべての攻撃は1つの関数hurt(humanoid, amount)を通るので、ダメージに関するルール(まもなく追加するブロックなど)は一度書くだけで済みます。また、すでに倒されたHumanoidには手を出しません。
チャレンジ
中級Eキーで発動する地面への叩きつけ攻撃を追加しましょう。
swingの上に、hurt(humanoid, amount)を記述します。Healthが0でなければ、TakeDamage(amount)を呼び出します。Swingでは、TakeDamageとprintの代わりにこの関数を呼び出します。abilities.Slam(player, character, root)を追加します。GetPartBoundsInRadius(root.Position, Settings.SlamRadius, params)の範囲内にいる各Humanoid(自分自身を除き、それぞれ1回)に、hurt(humanoid, Settings.SlamDamage)を適用します。BattleClientで、"Slam"をEキーにバインドし、shownにも追加します。
「プレイ」を押し、ダミーのそばで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 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
humanoid:TakeDamage(Settings.SwordDamage)
print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
end
end
end
-- 名前ごとのアビリティと、それぞれの動作
local abilities = {}
-- プレイヤーのキャラクターは本人のデバイスが動かすため、ダッシュもそこで行う
function abilities.Dash(player)
remotes.Ability:FireClient(player, "Dash")
end
-- レッスン10:Ground Slamはここに追加する
-- クライアントは名前を指定してアビリティの使用を要求する。サーバーは、実在する
-- アビリティか、プレイヤーが生存しているか、クールダウンが終わっているかを確認する。再び使用可能になる時刻は
-- プレイヤーの属性に保存されるため、画面に表示できる。
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プレイグラウンド