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で、printをabilitiesテーブルに置き換えます。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 = {} -- 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回の振りにつき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
-- クライアントが名前を指定してアビリティを要求する
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プレイグラウンド