Menu
CoddyTech

移動するレーザー

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

Lasers ステージには、Slider と Kill というタグが付いた赤い壁が2枚あり、通路を横切って移動し、元の位置に戻ります。それぞれに Slide 属性があり、開始位置からどこへ移動するかを示す Vector3 です。

トゥイーンは繰り返し再生できます。TweenInfo.new には、時間、イージング、方向、繰り返す回数(-1 は無限に繰り返すことを意味します)、逆再生するかどうかを指定します。

local info = TweenInfo.new(2.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)
TweenService:Create(part, info, { Position = part.Position + part:GetAttribute("Slide") }):Play()

Linear は速度を一定に保つため、プレイヤーは動きを予測できます。デフォルトのイージングでは両端で速度が落ちます。滑らかに見えますが、タイミングを合わせるのは難しくなります。

challenge icon

チャレンジ

中級
  1. 上部付近で、TweenServiceを取得します。
  2. makeSlide(part)を記述します。パーツのPositionを、そのPositionにSlide属性を加えた値まで、Linearで、Settings.SlideTime秒かけてTweenし、無期限に繰り返して逆再生します。
  3. setupStage内で、Sliderタグが付いた各パーツに対してこれを呼び出します。

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

-- 回転する障害物はフレームごとに少しずつ回る
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
		if part:HasTag("Conveyor") then
			part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeed
		end
	end
end

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

-- モデルのコピーをタワーの上に置き、コピーと新しい頂点の高さを返す
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

-- 新しいランダムなタワーを下から上へ組み立てる
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ゲーム:エンドレスタワーのすべてのレッスン