Menu
CoddyTech

回転装置

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

Spinner ステージには、通路を横切る赤いアームがあります。これは Spinner タグが付いた Model で、ピボットはハブにあります。毎フレーム Model 全体を少し回転させると、スピンします。

RunService.Heartbeat は毎フレーム 1 回発火し、前のフレームからの経過秒数(約 1/60)である dt を渡します。各フレームで speed * dt だけ回転させると、ゲームの実行速度に関係なく、1 秒ごとに合計で speed だけ回転します。

CFrame は位置と回転を合わせたものです。CFrame.Angles を掛けると回転し、角度はラジアン単位なので、math.rad を使って度から変換します。

spinner:PivotTo(spinner:GetPivot() * CFrame.Angles(0, math.rad(90) * dt, 0))

CollectionService:GetTagged("Spinner") は、ServerStorage 内のテンプレートを含め、タグが付いたものをすべて一覧表示するため、workspace 内にあるものだけを回転させます。アームには Kill タグが付いているため、すでにプレイヤーのライフを終わらせます。このレッスンでは、すべてのステージが Spinner です。

challenge icon

チャレンジ

中級
  1. 上部付近で、game:GetServiceを使ってCollectionServiceとRunServiceを取得します。
  2. RunService.Heartbeatに接続します。各フレームで、workspace内にありSpinnerタグが付けられたすべてのModelを、CFrame.Angles(0, math.rad(Settings.SpinSpeed) * dt, 0)だけピボットさせます。

Playを押して、アームのそばを通り抜けるタイミングを計るか、ジャンプで越えましょう。その後、Checkを押します。

自分で試してみよう

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
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 setupStage(stage)
	for _, part in stage:GetDescendants() do
		if part:HasTag("Kill") then
			part.Touched:Connect(killOnTouch)
		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ゲーム:エンドレスタワーのすべてのレッスン