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?

ダンボールに隠れるとAIから認識されないようにする

Last updated at Posted at 2025-03-23

敵の AI にダンボールを見逃すようにする

敵のスクリプト でプレイヤーが Box を装着していたら視認できないようにする。

前回のScriptを修正します
https://qiita.com/studyhiminato1107/items/286f48b958d9539fd110

レインボーフレンズのblueを使用

ツールボックス.png

レインボーフレンズのblueFollwoを編集

ブルー.png

Follow

local Players = game:GetService("Players")
local MAX_SEARCH_RADIUS = 50 -- 探索範囲(モンスターの探索半径を指定)
local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
local chasing = false  -- Blue がターゲットを追跡中かどうか
local currentTarget = nil  -- 現在のターゲット

-- Box をかぶっているか確認する関数
local function canSeePlayer(player)
	local char = player.Character
	if char then
		local box = char:FindFirstChild("Box")
		if box then
			print(player.Name .. " は Box をかぶっているため見逃される。")
			return false  -- ダンボールをかぶっているなら見えない
		else
			print(player.Name .. " は Box をかぶっていない。")
			return true  -- Box をかぶっていないなら見える
		end
	else
		print("キャラクターが見つかりません。")
	end
	return false  -- キャラクターがいない場合は見えない
end

-- 指定範囲内で最も近いプレイヤーを探す
local function findNearestPlayerInRadius(pos, radius)
	local players = Players:GetPlayers()
	local closestPlayer = nil
	local closestDistance = math.huge

	for _, player in ipairs(players) do
		local character = player.Character
		if character and character:FindFirstChild("HumanoidRootPart") and character:FindFirstChild("Humanoid") then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid and humanoid.Health > 0 then
				local humanoidRootPart = character.HumanoidRootPart
				local distance = (humanoidRootPart.Position - pos).Magnitude

				if distance < radius and distance < closestDistance and canSeePlayer(player) then
					closestPlayer = player
					closestDistance = distance
				end
			end
		end
	end

	return closestPlayer
end

while true do
	task.wait(0.1)  -- 負荷軽減のためのウェイト

	-- モンスターの現在位置を取得
	local currentPosition = script.Parent.HumanoidRootPart.Position

	-- 指定した範囲内で最も近いプレイヤーを探す
	local nearestPlayer = findNearestPlayerInRadius(currentPosition, MAX_SEARCH_RADIUS)

	if nearestPlayer then
		-- 探索範囲内でプレイヤーが見えている場合のみ追跡
		if canSeePlayer(nearestPlayer) then
			print("追跡中: プレイヤー", nearestPlayer.Name)
			currentTarget = nearestPlayer  -- 現在のターゲットを更新
			humanoid:MoveTo(nearestPlayer.Character.HumanoidRootPart.Position, nearestPlayer.Character)
			chasing = true
		else
			print(nearestPlayer.Name .. " は隠れているため追跡を停止します。")
			chasing = false
			currentTarget = nil
		end
	else
		-- 範囲内に追跡対象がいない場合
		if chasing then
			print("追跡対象を見失いました。")
			chasing = false
			currentTarget = nil
		else
			print("追跡対象がいません(範囲外)")
		end
	end
end

他にも必要な要素

  • LocalScriptの修正
  • RelicatedStorageToggleBoxEventを追加
  • ServerScriptServiceBoxHandlerを追加

スクリプト.png

LocalScriptの修正

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToggleBoxEvent = ReplicatedStorage:WaitForChild("ToggleBoxEvent")
local uis = game:GetService("UserInputService")

-- "B" キー入力で ToggleBoxEvent をサーバーに送信
uis.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.B then
		print("B キーが押されました。")
		ToggleBoxEvent:FireServer()  -- サーバーに送信
	end
end)

BoxHandler

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToggleBoxEvent = ReplicatedStorage:WaitForChild("ToggleBoxEvent")
local boxTemplate = ReplicatedStorage:FindFirstChild("BoxModel")

if not boxTemplate then
	warn("ReplicatedStorage に BoxModel が存在しません!")
	return
else
	print("BoxModel が見つかりました:", boxTemplate)
end

-- ToggleBoxEvent をサーバーで受け取ったときの処理
ToggleBoxEvent.OnServerEvent:Connect(function(player)
	local char = player.Character
	if not char then return end

	local humanoid = char:FindFirstChildOfClass("Humanoid")
	local head = char:FindFirstChild("Head")
	if not head then
		warn("キャラクターに Head がありません!")
		return
	end

	local existingBox = char:FindFirstChild("Box")

	if existingBox then
		-- 既にダンボールが装着されている場合は外す
		existingBox:Destroy()
		humanoid.WalkSpeed = 16
		print("Box を解除しました。WalkSpeed を 16 に戻しました。")
	else
		-- ダンボールを作成してキャラクターに装着
		local box = boxTemplate:Clone()
		box.Parent = char
		box.Name = "Box"

		local primaryPart = box:FindFirstChild("Part3")
		if not primaryPart then
			warn("BoxModel 内に Part3 が見つかりません。")
			return
		end

		box.PrimaryPart = primaryPart
		primaryPart.Anchored = false

		local heightOffset = 0.05
		local newCFrame = head.CFrame * CFrame.new(0, 1.5 + heightOffset, 0) * CFrame.Angles(math.rad(180), 0, 0)
		box:SetPrimaryPartCFrame(newCFrame)

		local weld = Instance.new("WeldConstraint")
		weld.Part0 = primaryPart
		weld.Part1 = head
		weld.Parent = primaryPart

		humanoid.WalkSpeed = 8
		print("Box を装着しました。WalkSpeed を 8 に低下しました。")
	end
end)
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?