Menu
CoddyTech

当たり判定

Coddyの「Robloxゲーム:バトルグラウンド」コースのレッスン 3/20。

攻撃は何かに当たらなければなりません。「自分の前に何があるか?」は空間クエリです。workspace:GetPartBoundsInBox はボックス(中心を表す CFrame とサイズを表す Vector3)を受け取り、そのボックスと重なるすべてのパーツを返します。

local box = root.CFrame * CFrame.new(0, 0, -3) -- 前方に3スタッド
local parts = workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params)

パーツのモデルには Humanoid が含まれているため、part.Parent:FindFirstChildOfClass("Humanoid") を使うと、そのパーツがダメージを受ける可能性のあるキャラクターに属しているかどうかがわかります。

自分の腕もそのボックスに入ります。OverlapParams はクエリをフィルタリングします。FilterDescendantsInstances に自分のキャラクターを指定すれば、ボックスが自分の体を検出することはありません。

challenge icon

チャレンジ

簡単

swingの最後に:

  1. プレイヤーのキャラクターを除外するOverlapParamsを作成します。
  2. ルートの3スタッド前にある、サイズがSettings.HitboxSizeのボックスについて、GetPartBoundsInBox内の各パーツを確認します。親にHumanoidがあれば、TakeDamage(Settings.SwordDamage)を実行します。

プレイを押して、トレーニングダミーを攻撃します。その後、確認します。

自分で試してみよう

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local lastSwing = {} -- player -> 最後に剣を振った時刻

-- Roblox標準の剣のアニメーション
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- 剣を振る動作:アニメーションはSwingCooldownごとに最大1回
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()
end

-- すべてのプレイヤーにリーダーボードのKillsと、復活するたびに剣を付与する
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats

	player.CharacterAdded:Connect(function()
		local sword = ServerStorage.Sword:Clone()
		sword.Parent = player.Backpack
		sword.Activated:Connect(function()
			swing(player)
		end)
	end)
end)
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

Robloxゲーム:バトルグラウンドのすべてのレッスン