This commit is contained in:
Leshyi committed 2026-11-02 16:38:04 +03:00
1 parent a213656689
commit 73e91faa63
2 files changed
+124 -5

No files matched your search

+118 -1
View File
@@ -1,4 +1,4 @@
-- Спидометр и подсказки за рулём, переворот багги на R и отсчёт автопереворота.
-- Спидометр и подсказки за рулём, переворот багги на R, отсчёт автопереворота и камера за рулём.
-- В бою (на карте команды) из багги не выйти: прыжок за рулём глушим, сервер всё равно посадит обратно.
local ContextActionService = game:GetService("ContextActionService")
@@ -121,3 +121,120 @@ UserInputService.InputBegan:Connect(function(input, processed)
Remotes.FlipBuggy:FireServer()
end
end)
-- ===== Камера за рулём =====
-- Держит багги по центру кадра, смотрит чуть сверху. Зажата ПКМ (или палец на экране) — можно осматриваться,
-- отпустил — камера плавно возвращается за машину. Колёсико — ближе/дальше. Стены и холмы между машиной
-- и камерой придвигают её ближе. Выход за край карты ограничивает EdgeFog.client.
local CAMERA_HEIGHT = 3 -- точка, на которую смотрим: центр багги и чуть выше
local DEFAULT_PITCH = math.rad(-16) -- наклон взгляда вниз по умолчанию
local PITCH_MIN, PITCH_MAX = math.rad(-70), math.rad(15)
local ZOOM_MIN, ZOOM_DEFAULT = 10, 20
local RETURN_RATE = 2.5 -- как быстро камера возвращается за машину (больше — быстрее)
local FOLLOW_RATE = 6 -- как плотно следует за поворотами машины
local SENSITIVITY = 0.005
local rotating = false
local yawOffset, pitch, zoom = 0, DEFAULT_PITCH, ZOOM_DEFAULT
local heading: number? = nil -- сглаженный курс машины
local driving = false
local function isRotateInput(input: InputObject): boolean
return input.UserInputType == Enum.UserInputType.MouseButton2 or input.UserInputType == Enum.UserInputType.Touch
end
UserInputService.InputBegan:Connect(function(input, processed)
if driving and isRotateInput(input) and not processed then
rotating = true
if input.UserInputType == Enum.UserInputType.MouseButton2 then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
end
end
end)
UserInputService.InputEnded:Connect(function(input)
if isRotateInput(input) and rotating then
rotating = false
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end
end)
UserInputService.InputChanged:Connect(function(input, processed)
if not driving then
return
end
if rotating and (input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch) then
yawOffset -= input.Delta.X * SENSITIVITY
pitch = math.clamp(pitch - input.Delta.Y * SENSITIVITY, PITCH_MIN, PITCH_MAX)
elseif input.UserInputType == Enum.UserInputType.MouseWheel and not processed then
zoom = math.clamp(zoom - input.Position.Z * 2.5, ZOOM_MIN, player.CameraMaxZoomDistance)
end
end)
-- Кратчайшая разница углов (чтобы не крутиться через лишний оборот)
local function angleDelta(from: number, to: number): number
return (to - from + math.pi) % (math.pi * 2) - math.pi
end
local function stopDrivingCamera()
driving = false
rotating = false
heading = nil
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
local camera = workspace.CurrentCamera
if camera.CameraType == Enum.CameraType.Scriptable and not player:GetAttribute("InGarage") then
camera.CameraType = Enum.CameraType.Custom
local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
camera.CameraSubject = humanoid
end
end
end
RunService:BindToRenderStep("BuggyCamera", Enum.RenderPriority.Camera.Value + 1, function(dt)
local seat = currentSeat()
local model = seat and seat.Parent
local chassis = model and (model :: Model).PrimaryPart
if not chassis then
if driving then
stopDrivingCamera()
end
return
end
if not driving then
driving = true
yawOffset, pitch = 0, DEFAULT_PITCH
zoom = math.clamp(ZOOM_DEFAULT, ZOOM_MIN, player.CameraMaxZoomDistance)
end
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
-- Курс машины по горизонтали, сглаженный — чтобы камеру не трясло на кочках и при заносах.
local look = chassis.CFrame.LookVector
local carYaw = math.atan2(-look.X, -look.Z)
if Vector3.new(look.X, 0, look.Z).Magnitude < 0.2 then
carYaw = heading or carYaw -- машина стоит на носу или на корме — курс не меняем
end
heading = if heading then heading + angleDelta(heading, carYaw) * math.min(dt * FOLLOW_RATE, 1) else carYaw
-- Без ПКМ — плавно возвращаемся за машину и к обычному наклону.
if not rotating then
local k = math.min(dt * RETURN_RATE, 1)
yawOffset += angleDelta(yawOffset, 0) * k
pitch += (DEFAULT_PITCH - pitch) * k
end
local target = chassis.Position + Vector3.new(0, CAMERA_HEIGHT, 0)
local rotation = CFrame.Angles(0, heading + yawOffset, 0) * CFrame.Angles(pitch, 0, 0)
local distance = zoom
-- Препятствие между машиной и камерой — придвигаемся
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = { model, player.Character }
local back = rotation.LookVector * -zoom
local hit = workspace:Raycast(target, back, params)
if hit then
distance = math.max((hit.Position - target).Magnitude - 1, 3)
end
camera.CFrame = CFrame.new(target) * rotation * CFrame.new(0, 0, distance)
camera.Focus = CFrame.new(target)
end)
+6 -4
View File
@@ -5,6 +5,7 @@
-- Гаражи далеко за картами — там ничего не трогаем.
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
@@ -43,11 +44,12 @@ local function edgeFactor(position: Vector3): number
return math.clamp(1 - toEdge / FOG_RANGE, 0, 1)
end
-- После модуля камеры: возвращаем камеру внутрь карты и под туманный потолок, направление взгляда не меняем.
RunService:BindToRenderStep("KeepCameraInZone", Enum.RenderPriority.Camera.Value + 1, function()
-- После модуля камеры и камеры за рулём (Driving.client): возвращаем камеру внутрь карты
-- и под туманный потолок, направление взгляда не меняем.
RunService:BindToRenderStep("KeepCameraInZone", Enum.RenderPriority.Camera.Value + 2, function()
local camera = workspace.CurrentCamera
if camera.CameraType == Enum.CameraType.Scriptable then
return -- меню гаража и заставки ставят камеру сами
if Players.LocalPlayer:GetAttribute("InGarage") then
return -- меню гаража ставит камеру само
end
local cf = camera.CFrame
local center = mapCenter(cf.Position)