0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

60秒で作る!プレイヤーを追いかけるPart

Posted at

✅ この教材で学べること

  • RunService.Heartbeat の使い方
  • プレイヤーキャラの検索方法
  • Vector3で距離を求める方法
  • CFrameの加算で動かす方法

🧱 準備するもの

項目 内容
1 Roblox Studio を起動する
2 Scene に「Part」をひとつ置く(名前変更不要)
3 その Part に「Script」を挿入する(右クリック → Insert Object → Script)

🧠 スクリプト全体

-- 対象となるPartを取得
local part = script.Parent

-- Heartbeat: 毎フレームごとに処理を繰り返すイベント
game:GetService("RunService").Heartbeat:Connect(function()

	-- 一番近いプレイヤーを探すための変数
	local closestPlayer = nil
	local shortestDistance = math.huge -- 初期値は超デカい数(=無限大)

	-- 全プレイヤーをチェック
	for _, player in pairs(game.Players:GetPlayers()) do
		local char = player.Character
		if char and char:FindFirstChild("HumanoidRootPart") then
			local dist = (char.HumanoidRootPart.Position - part.Position).Magnitude
			if dist < shortestDistance then
				shortestDistance = dist
				closestPlayer = char.HumanoidRootPart
			end
		end
	end

	-- 一番近かったプレイヤーに向かって移動する
	if closestPlayer then
		local direction = (closestPlayer.Position - part.Position).Unit
		part.CFrame = part.CFrame + direction * 0.5 -- 距離に応じて動かす(0.5はスピード)
	end
end)

🔍 解説ポイント

行数 説明
1 script.Parent はこのスクリプトが入ってるPart自体のこと。
3 RunService.Heartbeat は毎フレーム動作。ゲームの心臓みたいなもの。
6〜15 プレイヤーを1人ずつ調べて、**「いちばん近い人」**を見つけるループ。
17〜20 近いプレイヤーに向かって移動。Unitは方向を1に揃える処理。

💡 アドバイス

  • direction * 0.5* 0.2* 1.0 に変えるとスピードが変わるよ。
  • TweenService を使えば、もっと滑らかで柔らかい動きにもできる(上級編)。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?