Plots rebondissants
Leçon 9 sur 18 du cours Jeu Roblox : tour sans fin de Coddy.
Le stage Bounce comporte une plateforme verte, marquée Bounce, sous un rebord trop haut pour être atteint en sautant. En atterrissant sur la plateforme, tu devrais être propulsé dessus.
Un personnage se déplace avec son HumanoidRootPart, la partie invisible située au milieu de son corps. Définis la valeur de AssemblyLinearVelocity de cette partie, et le personnage s'envole dans cette direction ; la gravité le fait ensuite retomber :
root.AssemblyLinearVelocity = Vector3.new(0, 60, 0) -- 60 studs par seconde, tout droit vers le hautTouched te renvoie le membre qui a touché la plateforme, généralement une jambe. Son parent est le personnage, qui contient le HumanoidRootPart.
Défi
Moyen- Écrivez
bounce(hit): si le modèle de celui qui touche possède unHumanoidRootPart, définissez sonAssemblyLinearVelocitysurVector3.new(0, Settings.BouncePower, 0). - Dans
setupStage, connectez l’événement Touched de chaque partie marquéeBounceà cette fonction.
Appuyez sur Play, atterrissez sur le pad et dirigez-vous vers le rebord. Puis cliquez sur Check.
Essayez vous-même
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Settings = require(game.ReplicatedStorage.Settings)
local stages = ServerStorage.Stages
local tower = nil
print("Each tower has " .. Settings.StageCount .. " stages")
-- Chaque joueur reçoit Wins dans le classement
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)
-- Tout objet portant le tag Kill met fin à une vie
local function killOnTouch(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Une marche escamotable s’estompe peu après qu’une personne marche dessus, puis réapparaît
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
-- Un laser glisse jusqu’au décalage Slide, puis revient, indéfiniment
local function makeSlide(part)
local info = TweenInfo.new(Settings.SlideTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)
local goal = { Position = part.Position + part:GetAttribute("Slide") }
TweenService:Create(part, info, goal):Play()
end
-- Les Spinners tournent un peu à chaque image
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)
-- Active tous les obstacles d’un niveau selon leurs tags
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
if part:HasTag("Slider") then
makeSlide(part)
end
end
end
-- Un niveau quelconque, choisi au hasard
local function pickStage()
local all = stages:GetChildren()
return all[math.random(1, #all)]
end
-- Place une copie d’un modèle au sommet de la tour ; renvoie la copie et la nouvelle hauteur du sommet
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
-- Construis une nouvelle tour aléatoire de bas en haut
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()
Cette leçon comprend un petit quiz. Commencez la leçon pour y répondre et suivre votre progression.
Toutes les leçons de Jeu Roblox : tour sans fin
2Obstacles
Le Tourniquet