como fazer player voar

Como fazer o Player Voar

Já imaginou criar um jogo onde os jogadores podem levantar voo e explorar o mapa pelo ar? No Roblox Studio, é possível adicionar essa mecânica de forma simples e divertida! 🎮✨

Neste post, vou te mostrar como fazer o player voar no Roblox, adicionando um sistema que permite que os jogadores voem livremente e, para deixar tudo ainda mais incrível, vamos incluir um efeito de rastro no ar, dando um toque profissional ao seu jogo! 💨🔥

Além disso, você encontrará aqui os scripts completos para implementar essa funcionalidade no seu jogo. Então, continue lendo e vamos colocar a mão na massa! 🚀

Assista o Vídeo para Fazer o Player Voar

ServerScript

				
					local Players = game:GetService("Players")


local ReplicatedStorage = game:GetService("ReplicatedStorage")




Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		task.wait(1)
		local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
		local trailAttachment0 = Instance.new("Attachment")
		trailAttachment0.Name  = "TrailAttachment0"
		trailAttachment0.Parent= character.HumanoidRootPart
		
		local trailAttachment1= Instance.new("Attachment")
		trailAttachment1.Name = "TrailAttachment1"
		trailAttachment1.Position = trailAttachment0.Position + Vector3.new(0,2,2)
		trailAttachment1.Parent = character.HumanoidRootPart
		
		local trail = ReplicatedStorage.Trail:Clone()
		trail.Attachment0 = humanoidRootPart.TrailAttachment0
		trail.Attachment1 = humanoidRootPart.TrailAttachment1
		trail.Parent= character.HumanoidRootPart
		
	end)
end)

				
			

LocalScript

				
					local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")


local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera

local trail = humanoidRootPart:WaitForChild("Trail")
trail.Enabled = false

local flying = false
local speed = 80
local flightVector = Vector3.new(0,0,0)
local originalCFrame 
local originalOrientation = CFrame.new()


local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(10000,10000,10000)
BodyVelocity.Velocity = Vector3.new(0,0,0)


local BodyGyro = Instance.new("BodyGyro")
BodyGyro.MaxTorque = Vector3.new(10000,10000,10000)
BodyGyro.P = 10000
BodyGyro.D = 100

local function updateFlight()
	local forward = camera.CFrame.LookVector
	local right = camera.CFrame.RightVector
	local up = Vector3.new(0,1,0)
	
	flightVector = Vector3.new(0,0,0)
	
	if UserInputService:IsKeyDown(Enum.KeyCode.W) then flightVector += forward	end
	if UserInputService:IsKeyDown(Enum.KeyCode.S) then flightVector -= forward end
	if UserInputService:IsKeyDown(Enum.KeyCode.A) then flightVector -= right end
	if UserInputService:IsKeyDown(Enum.KeyCode.D) then flightVector += right end
	if UserInputService:IsKeyDown(Enum.KeyCode.E) then flightVector += up end
	if UserInputService:IsKeyDown(Enum.KeyCode.Q) then flightVector -= up end
	
	if flightVector.Magnitude > 0 then
		flightVector = flightVector.Unit * speed
	end
end

local function startFlying()
	if flying then
		return
	end
	flying = true
	trail.Enabled = true
	originalCFrame= humanoidRootPart.CFrame
	originalOrientation = humanoidRootPart.CFrame
	humanoid.PlatformStand= true
	BodyGyro.CFrame = camera.CFrame * CFrame.Angles(-math.pi/2, 0,0)
	BodyVelocity.Parent = humanoidRootPart
	BodyGyro.Parent = humanoidRootPart
	
	RunService.Heartbeat:Connect(function()
		if flying then
			BodyGyro.CFrame = camera.CFrame * CFrame.Angles(-math.pi/2, 0,0)
			updateFlight()
			BodyVelocity.Velocity = flightVector
		end
	end)
end



local function stopFlying()
	if not flying then return end
	flying= false  
	trail.Enabled = false
	humanoid.PlatformStand = false
	BodyVelocity.Parent = nil
	BodyGyro.Parent = nil
	humanoidRootPart.CFrame = originalCFrame
	
end
local function checkForGround()
	local baseplate = workspace:FindFirstChild("Baseplate")
	if baseplate then
		local touched = humanoidRootPart.Position.Y <= baseplate.Position.Y +5
		if touched then
			stopFlying()
		end
	end
end

RunService.Heartbeat:Connect(function()
	if flying then
		checkForGround()
	end
end)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode== Enum.KeyCode.Space then
		if flying then
			-- stop flying	
			stopFlying()
		else
			-- start flying
			startFlying()
		end
	end
end)



workspace.Baseplate.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == player.Name then
		if flying then
			stopFlying()
		end
	end
end)
				
			

Quer aprender mais?

Se você gosta de programação e quer criar seus próprios jogos, conheça os cursos da Programação for Kids! 🎓🚀 Aqui, crianças e adolescentes aprendem a programar de forma fácil e divertida, criando jogos no Roblox e muito mais!

Leave a Reply

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *