Menu
CoddyTech

コンベアベルト

Coddyの「Robloxゲーム:エンドレスタワー」コースのレッスン 7/18。

コンベヤーステージには、Conveyorというタグが付いた暗い色のベルトがあり、来た方向へ押し戻されます。アンカーされたパーツは動きませんが、AssemblyLinearVelocityを設定すると、その上に立っているものは本物のベルトのように運ばれます。

どちらの方向でしょうか? すべてのパーツには正面があり、part.CFrame.LookVectorはそこから外向きに伸びる長さ1スタッドのVector3です。ベルトは、通路を戻る方向に正面が向くように回転されています。

part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeed

パーツ自体の向きを使うため、スクリプトを変更しなくても、作成者はベルトをどの方向にも向けられ、正しく動作します。

challenge icon

チャレンジ

中級

setupStage内で、Conveyorタグが付いたすべてのパーツに、CFrame.LookVectorとSettings.BeltSpeedを掛けた値をAssemblyLinearVelocityとして設定します。

Playを押し、ベルトに逆らって歩きます。その後、Checkを押します。

自分で試してみよう

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

local stages = ServerStorage.Stages
local tower = nil

print("Each tower has " .. Settings.StageCount .. " stages")

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

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- Killタグが付いたものに触れると命を失う
local function killOnTouch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- 消える足場は、誰かが乗るとすぐに薄くなり、その後元に戻る
local function makeVanish(part)
	part.Touched:Connect(function(hit)
		if part:GetAttribute("Gone") or not hit.Parent:FindFirstChild("Humanoid") then
			return
		end
		part:SetAttribute("Gone", true)
		task.wait(Settings.VanishDelay)
		part.Transparency = 0.8
		part.CanCollide = false
		task.wait(Settings.VanishTime)
		part.Transparency = 0
		part.CanCollide = true
		part:SetAttribute("Gone", nil)
	end)
end

-- Spinnersは毎フレーム少しずつ回転する
RunService.Heartbeat:Connect(function(dt)
	for _, spinner in CollectionService:GetTagged("Spinner") do
		if spinner:IsDescendantOf(workspace) then
			spinner:PivotTo(spinner:GetPivot() * CFrame.Angles(0, math.rad(Settings.SpinSpeed) * dt, 0))
		end
	end
end)

-- タグに応じて、ステージ内のすべての障害物を有効にする
local function setupStage(stage)
	for _, part in stage:GetDescendants() do
		if part:HasTag("Kill") then
			part.Touched:Connect(killOnTouch)
		end
		if part:HasTag("Vanish") then
			makeVanish(part)
		end
	end
end

-- ステージをランダムに1つ選ぶ
local function pickStage()
	local all = stages:GetChildren()
	return all[math.random(1, #all)]
end

-- モデルのコピーをtowerの上に配置し、コピーと新しい頂点の高さを返す
local function stack(template, bottom)
	local copy = template:Clone()
	local box, size = copy:GetBoundingBox()
	local lift = bottom - (box.Position.Y - size.Y / 2)
	copy:PivotTo(copy:GetPivot() + Vector3.new(0, lift, 0))
	copy.Parent = tower
	return copy, bottom + size.Y
end

-- 下から順に、ランダムな新しいtowerを組み立てる
local function buildTower()
	tower = Instance.new("Model")
	tower.Name = "Tower"
	tower.Parent = workspace
	local top = 0
	for i = 1, Settings.StageCount do
		local stage, newTop = stack(pickStage(), top)
		top = newTop
		setupStage(stage)
	end
	stack(ServerStorage.Finish, top)
end

buildTower()
quiz icon腕試し

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

Robloxゲーム:エンドレスタワーのすべてのレッスン