Menu
CoddyTech

绕着墙走

Coddy Roblox 游戏:逃离黑暗 课程的第 11 课,共 25 课。

站在房间里,靠近门的位置,怪物却走进了墙里:MoveTo 会沿直线移动。做出两处改动,它就会变成猎手。

首先,它只会在 Settings.MonsterSight 范围内追捕。其次,遇到墙挡路时,它会寻找路径。workspace:Raycast 会从怪物向你发射一条射线:没有击中任何东西(或者击中你),就表示道路畅通。否则,PathfindingService 会找到一条绕过墙壁、穿过门口的路径,怪物会走向路径上的下一个点,并在四分之一秒后再次查询:

path:ComputeAsync(root.Position, goal)
local points = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success and points[2] then
	shadow.Humanoid:MoveTo(points[2].Position)
end
challenge icon

挑战

中等

让 Shadow 找到绕过墙壁的路。

  1. 将 nearestCharacter 的最佳距离设为 Settings.MonsterSight。
  2. 编写 walkToward(shadow, goal, path):对目标进行射线检测,并排除怪物;如果没有命中,或命中角色,就直线行走。
  3. 否则,调用 ComputeAsync,并走向 points[2]。
  4. 在 runMonster 中创建 PathfindingService:CreatePath(),并用 walkToward 追赶目标;如果范围内没有人,就调用 MoveTo(root.Position)。

按“开始”,站在门口旁边:Shadow 会绕过墙壁。然后按“检查”。

亲手试试

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

local rooms = workspace.Rooms
local lastRoom = rooms.Lobby -- 最新的房间
local count = 0 -- 目前已创建的房间数
local monster = nil

-- 夜幕降临:没有阳光,四处有微光,还起了雾
Lighting.ClockTime = Settings.ClockTime
Lighting.Ambient = Settings.Ambient
Lighting.OutdoorAmbient = Settings.Ambient
Lighting.FogColor = Color3.new(0, 0, 0)
Lighting.FogEnd = Settings.FogEnd
print("Lights out")

-- 玩家携带的工具:拿在手中,或放在 Backpack 里
local function findTool(player, name)
	return (player.Character and player.Character:FindFirstChild(name)) or player.Backpack:FindFirstChild(name)
end

-- 让门绕着铰链旋转打开
local function openDoor(door)
	door:SetAttribute("Open", true)
	door.Panel.Prompt.Enabled = false
	local hinge, panel = door.Hinge, door.Panel
	local goal = hinge.CFrame * CFrame.Angles(0, math.rad(90), 0) * hinge.CFrame:ToObjectSpace(panel.CFrame)
	TweenService:Create(panel, TweenInfo.new(0.6), { CFrame = goal }):Play()
end

-- 最近的存活玩家,以及与其相距多远
local function nearestCharacter(position)
	local best, bestDistance = nil, math.huge
	for _, player in Players:GetPlayers() do
		local character = player.Character
		local root = character and character:FindFirstChild("HumanoidRootPart")
		if root and character.Humanoid.Health > 0 then
			local distance = (root.Position - position).Magnitude
			if distance < bestDistance then
				best, bestDistance = character, distance
			end
		end
	end
	return best, bestDistance
end

-- 第 11 课:Around the Walls 的代码写在这里

-- 怪物的行动逻辑:每隔四分之一秒追逐一名玩家
local function runMonster(shadow)
	local humanoid = shadow.Humanoid
	local root = shadow.HumanoidRootPart
	while shadow.Parent do
		local character = nearestCharacter(root.Position)
		if character then
			humanoid:MoveTo(character.HumanoidRootPart.Position)
		end
		task.wait(0.25)
	end
end

-- 怪物从你身后出现:位置是最新房间往前数两个房间的入口
local function spawnMonster()
	local behind = nil
	for _, room in rooms:GetChildren() do
		if room:GetAttribute("Number") == count - 2 then
			behind = room
		end
	end
	if not behind then
		return
	end
	monster = ServerStorage.Shadow:Clone()
	monster:PivotTo(behind.Entrance.CFrame + Vector3.new(0, 3.1, 0))
	monster.Humanoid.WalkSpeed = Settings.MonsterSpeed
	monster.Parent = workspace
	task.spawn(runMonster, monster)
end

local addRoom -- 在下方编写:开门会添加房间,而房间里有门

-- 玩家打开门时,门会做什么
local function setupDoor(door)
	door.Panel.Prompt.Triggered:Connect(function(player)
		if door:GetAttribute("Open") then
			return
		end
		if door:GetAttribute("Locked") then
			local key = findTool(player, "Key")
			if not key then
				return
			end
			key:Destroy()
			door:SetAttribute("Locked", nil)
		end
		if door.Parent == lastRoom then
			addRoom()
		end
		openDoor(door)
	end)
end
setupDoor(workspace.Rooms.Lobby.Door)

-- 走进房间:它会成为玩家的 Room 和复活点
local function setupEntry(room)
	room.Entry.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local number = room:GetAttribute("Number")
			player.leaderstats.Room.Value = number
			player.RespawnLocation = room.Checkpoint
		end
	end)
end
setupEntry(workspace.Rooms.Lobby)

-- 在最新房间的出口处生成一个新房间
function addRoom()
	count += 1
	local name = Settings.Order[(count - 1) % #Settings.Order + 1]
	if count >= Settings.RoomsToEscape then
		name = "Exit"
	end
	local room = ServerStorage.Rooms[name]:Clone()
	room:PivotTo(lastRoom.Exit.CFrame)
	room:SetAttribute("Number", count)
	room.Sign.SurfaceGui.Number.Text = "Room " .. count
	room.Parent = rooms
	lastRoom = room
	setupDoor(room.Door)
	setupEntry(room)
	-- 上锁的房间里放着它的钥匙
	local keySpot = room:FindFirstChild("KeySpot")
	if keySpot then
		local key = ServerStorage.Key:Clone()
		key.Handle.CFrame = keySpot.CFrame
		key.Parent = room
		key.Handle.Prompt.Triggered:Connect(function(player)
			key.Handle.Prompt.Enabled = false
			key.Parent = player.Backpack
		end)
	end
	-- 保留最新房间,以及它前面的 KeepRooms 个房间
	for _, old in rooms:GetChildren() do
		local number = old:GetAttribute("Number")
		if number < count - Settings.KeepRooms then
			print("Room " .. number .. " is gone")
			old:Destroy()
		end
	end
	if count == Settings.MonsterFrom then
		spawnMonster()
	end
	return room
end

-- 每位玩家:排行榜上有 Room,每次重生都会获得手电筒
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local room = Instance.new("IntValue")
	room.Name = "Room"
	room.Parent = leaderstats

	player.CharacterAdded:Connect(function()
		local flashlight = ServerStorage.Flashlight:Clone()
		flashlight.Parent = player.Backpack
		local beam = flashlight.Handle.Beam
		flashlight.Activated:Connect(function()
			beam.Enabled = not beam.Enabled
		end)
	end)
end)

Roblox 游戏:逃离黑暗中的所有课

自己练习: Roblox 在线编辑器