350 lines
13 KiB
Luau
350 lines
13 KiB
Luau
--!strict
|
|
-- Third-person rifle pose: how the SniperRifle looks in the hands of a character seen from outside.
|
|
-- The server (ServerScriptService.ThirdPersonRifle) welds the rifle to the UpperTorso; this script
|
|
-- runs on every client for every character holding the rifle and bends both arms onto it with
|
|
-- IKControls: the right hand to the pistol grip, the left hand to the handguard, elbows toward
|
|
-- pole points next to the torso. Same values on every client, so all players see the same pose.
|
|
-- The inspect animation is first-person only and is never shown here; the reload is (other
|
|
-- players' reloads arrive through ReplicatedStorage.SniperReload, the own one through the
|
|
-- ReloadStamp attribute set by SniperViewmodel). Shots (ReplicatedStorage.SniperShot) show a
|
|
-- muzzle flash and a small kick of the rifle, and the bullet hole for other players' shots.
|
|
-- Tuned through attributes on PlayerScripts.SniperViewmodel (prefix TP), live-editable with the
|
|
-- ViewmodelTuner panel (P); its "Вид" button switches the local camera to third person.
|
|
|
|
local Players = game:GetService("Players")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local RunService = game:GetService("RunService")
|
|
|
|
local TOOL_NAME = "SniperRifle"
|
|
local WELD_NAME = "TPRifleWeld"
|
|
local player = Players.LocalPlayer
|
|
local config = player:WaitForChild("PlayerScripts"):WaitForChild("SniperViewmodel")
|
|
local ReloadTimeline = require(ReplicatedStorage:WaitForChild("ReloadTimeline") :: ModuleScript)
|
|
local reloadRemote = ReplicatedStorage:WaitForChild("SniperReload") :: RemoteEvent
|
|
local shotRemote = ReplicatedStorage:WaitForChild("SniperShot") :: RemoteEvent
|
|
local SniperEffects = require(ReplicatedStorage:WaitForChild("SniperEffects") :: ModuleScript)
|
|
local RifleSkin = require(ReplicatedStorage:WaitForChild("RifleSkin") :: ModuleScript)
|
|
local SkinCatalog = require(ReplicatedStorage:WaitForChild("RifleSkinCatalog") :: ModuleScript)
|
|
|
|
-- All positions in studs, angles in degrees.
|
|
-- TPGun*: rifle grip (Handle) relative to the UpperTorso (+X right, +Y up, -Z forward).
|
|
-- TPRHand* / TPLHand*: where each hand grabs the rifle, in rifle space (-Z = barrel, +Y = up).
|
|
-- TPRElbow* / TPLElbow*: points the elbows bend toward, relative to the UpperTorso.
|
|
local DEFAULTS = {
|
|
TPGunX = 0.45,
|
|
TPGunY = -0.25,
|
|
TPGunZ = -0.8,
|
|
TPGunYawDeg = 0,
|
|
TPGunPitchDeg = 0,
|
|
TPGunRollDeg = 0,
|
|
TPRHandX = 0,
|
|
TPRHandY = 0,
|
|
TPRHandZ = 0,
|
|
TPRHandYawDeg = 0,
|
|
TPRHandPitchDeg = 0,
|
|
TPRHandRollDeg = 0,
|
|
TPLHandX = 0,
|
|
TPLHandY = 0.1,
|
|
TPLHandZ = -0.6,
|
|
TPLHandYawDeg = 0,
|
|
TPLHandPitchDeg = 0,
|
|
TPLHandRollDeg = 0,
|
|
TPRElbowX = 2,
|
|
TPRElbowY = -1.5,
|
|
TPRElbowZ = 0.5,
|
|
TPLElbowX = -2,
|
|
TPLElbowY = -1.5,
|
|
TPLElbowZ = 0,
|
|
-- reload, rifle space (the third-person rifle is about half the viewmodel's size)
|
|
TPMagX = 0,
|
|
TPMagY = 0.03,
|
|
TPMagZ = -0.49,
|
|
TPMagDrop = 0.8,
|
|
TPBoltX = 0.19,
|
|
TPBoltY = 0.46,
|
|
TPBoltZ = -0.05,
|
|
TPBoltLift = 0.08,
|
|
TPBoltPull = 0.25,
|
|
-- shot
|
|
TPMuzzleX = 0, -- muzzle end, rifle space
|
|
TPMuzzleY = 0.43,
|
|
TPMuzzleZ = -2.19,
|
|
TPFlashSize = 0.6,
|
|
TPRecoilBack = 0, -- automatic rifle: the weapon does not jump
|
|
TPRecoilPitchDeg = 0,
|
|
}
|
|
for name, value in DEFAULTS do
|
|
if config:GetAttribute(name) == nil then
|
|
config:SetAttribute(name, value)
|
|
end
|
|
end
|
|
local function attr(name: string): number
|
|
-- ReloadTime / ReloadPreviewU belong to SniperViewmodel, which sets them on start
|
|
return (config:GetAttribute(name) :: number?) or (DEFAULTS :: any)[name] or 0
|
|
end
|
|
local function pose(prefix: string): CFrame
|
|
return CFrame.new(attr(prefix .. "X"), attr(prefix .. "Y"), attr(prefix .. "Z"))
|
|
* CFrame.Angles(0, math.rad(attr(prefix .. "YawDeg")), 0)
|
|
* CFrame.Angles(math.rad(attr(prefix .. "PitchDeg")), 0, 0)
|
|
* CFrame.Angles(0, 0, math.rad(attr(prefix .. "RollDeg")))
|
|
end
|
|
local function point(prefix: string): Vector3
|
|
return Vector3.new(attr(prefix .. "X"), attr(prefix .. "Y"), attr(prefix .. "Z"))
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- per-character arm IK
|
|
type Arm = { key: string, target: Attachment, pole: Attachment }
|
|
type Rig = {
|
|
handle: BasePart, arms: { Arm }, extra: { Instance }, flames: RifleSkin.Effect?,
|
|
skinChanged: RBXScriptConnection?,
|
|
}
|
|
local rigs: { [Model]: Rig } = {}
|
|
local reloadStarts: { [Model]: {start: number, duration: number} } = {}
|
|
|
|
-- Reload time (0..1) of a character, or nil when it is not reloading.
|
|
local function reloadU(character: Model): number?
|
|
if character == player.Character and config:GetAttribute("ReloadPreview") == true then
|
|
return math.clamp(attr("ReloadPreviewU"), 0, 1) -- tuner's frozen frame
|
|
end
|
|
local reload = reloadStarts[character]
|
|
if not reload then
|
|
return nil
|
|
end
|
|
local u = (os.clock() - reload.start) / math.max(reload.duration, 0.1)
|
|
if u >= 1 then
|
|
reloadStarts[character] = nil
|
|
return nil
|
|
end
|
|
return u
|
|
end
|
|
|
|
local function handPose(key: string, u: number?): CFrame
|
|
local base = pose(key .. "Hand")
|
|
if not u then
|
|
return base
|
|
end
|
|
local position = if key == "TPL"
|
|
then ReloadTimeline.leftHand(u, base.Position, point("TPMag"), attr("TPMagDrop"))
|
|
else ReloadTimeline.rightHand(u, base.Position, point("TPBolt"), attr("TPBoltLift"), attr("TPBoltPull"))
|
|
return base.Rotation + position
|
|
end
|
|
|
|
-- the rifle of a character, as the place its sounds come from
|
|
local function rifleHandle(character: Model): BasePart?
|
|
local tool = character:FindFirstChild(TOOL_NAME)
|
|
local handle = tool and tool:FindFirstChild("Handle")
|
|
return if handle and handle:IsA("BasePart") then handle else nil
|
|
end
|
|
|
|
-- Another player's sounds, from their rifle: each cue at its moment, unless `still` says the
|
|
-- action was cut short (the own player's sounds come from SniperViewmodel instead).
|
|
local function scheduleCues(character: Model, list: { any }, delay: number, length: number, still: () -> boolean)
|
|
for _, cue in list do
|
|
task.delay(delay + cue.at * length, function()
|
|
local handle = rifleHandle(character)
|
|
if handle and still() then
|
|
SniperEffects.sound(cue.sound, handle)
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
reloadRemote.OnClientEvent:Connect(function(character: Model, duration: number)
|
|
if typeof(character) == "Instance" and character:IsA("Model") then
|
|
local reload = {start = os.clock(), duration = if typeof(duration) == "number" then duration else attr("ReloadTime")}
|
|
reloadStarts[character] = reload
|
|
scheduleCues(character, SniperEffects.RELOAD_CUES, 0, reload.duration, function()
|
|
return reloadStarts[character] == reload
|
|
end)
|
|
end
|
|
end)
|
|
config:GetAttributeChangedSignal("ReloadStamp"):Connect(function()
|
|
local character = player.Character
|
|
if character then
|
|
reloadStarts[character] = {
|
|
start = os.clock(),
|
|
duration = (config:GetAttribute("ReloadDuration") :: number?) or attr("ReloadTime"),
|
|
}
|
|
end
|
|
end)
|
|
|
|
local function makeRig(character: Model, tool: Tool, handle: BasePart, torso: BasePart): Rig
|
|
local rig: Rig = { handle = handle, arms = {}, extra = {} }
|
|
local function updateSkin()
|
|
local choice = SkinCatalog.get(tool:GetAttribute("Skin"))
|
|
RifleSkin.destroy(rig.flames)
|
|
rig.flames = RifleSkin.apply(tool, handle, choice.id, choice.accent)
|
|
end
|
|
updateSkin()
|
|
rig.skinChanged = tool:GetAttributeChangedSignal("Skin"):Connect(updateSkin)
|
|
local humanoid = character:FindFirstChildOfClass("Humanoid")
|
|
for _, side in { "Right", "Left" } do
|
|
local target = Instance.new("Attachment")
|
|
target.Name = "TP" .. side .. "HandTarget"
|
|
target.Parent = handle
|
|
local pole = Instance.new("Attachment")
|
|
pole.Name = "TP" .. side .. "ElbowPole"
|
|
pole.Parent = torso
|
|
table.insert(rig.extra, target)
|
|
table.insert(rig.extra, pole)
|
|
local upper = character:FindFirstChild(side .. "UpperArm")
|
|
local hand = character:FindFirstChild(side .. "Hand")
|
|
if humanoid and upper and hand then -- R15 only; R6 arms have no joints to bend
|
|
local c = Instance.new("IKControl")
|
|
c.Name = "TP" .. side .. "ArmIK"
|
|
c.Type = Enum.IKControlType.Transform
|
|
c.ChainRoot = upper
|
|
c.EndEffector = hand:FindFirstChild(side .. "GripAttachment") or hand
|
|
c.Target = target
|
|
c.Pole = pole
|
|
c.Weight = 1
|
|
c.SmoothTime = 0
|
|
c.Parent = humanoid
|
|
table.insert(rig.extra, c)
|
|
end
|
|
table.insert(rig.arms, { key = if side == "Right" then "TPR" else "TPL", target = target, pole = pole })
|
|
end
|
|
return rig
|
|
end
|
|
|
|
-- ---------------------------------------------------------------- shots
|
|
local shotStarts: { [Model]: number } = {}
|
|
|
|
-- 0..1 kick of a character's rifle: snaps back fast, settles in 0.3 s.
|
|
local function kick(character: Model): number
|
|
local start = shotStarts[character]
|
|
if not start then
|
|
return 0
|
|
end
|
|
local t = os.clock() - start
|
|
if t >= 0.34 then
|
|
shotStarts[character] = nil
|
|
return 0
|
|
end
|
|
if t < 0.04 then
|
|
return t / 0.04
|
|
end
|
|
return 1 - ReloadTimeline.smooth((t - 0.04) / 0.3)
|
|
end
|
|
|
|
shotRemote.OnClientEvent:Connect(function(character: Model, hitPosition: Vector3?, hitNormal: Vector3?, flesh: boolean?)
|
|
if typeof(character) ~= "Instance" or not character:IsA("Model") then
|
|
return
|
|
end
|
|
local own = character == player.Character
|
|
if typeof(hitPosition) == "Vector3" and typeof(hitNormal) == "Vector3" then
|
|
SniperEffects.impact(hitPosition, hitNormal, flesh == true)
|
|
end
|
|
-- the shot and the bolt after it, heard from the shooter's rifle
|
|
local shooterHandle = rifleHandle(character)
|
|
if not own and shooterHandle then
|
|
SniperEffects.sound("Shot", shooterHandle)
|
|
if attr("ShotBoltTime") > 0 then -- 0 = automatic, no bolt
|
|
scheduleCues(character, SniperEffects.BOLT_CUES, attr("ShotBoltDelay"), attr("ShotBoltTime"), function()
|
|
return reloadStarts[character] == nil -- a reload cuts the bolt short
|
|
end)
|
|
end
|
|
end
|
|
-- in first person the shooter sees the viewmodel's flash instead
|
|
if own and config:GetAttribute("ThirdPersonView") ~= true then
|
|
return
|
|
end
|
|
shotStarts[character] = os.clock()
|
|
local rig = rigs[character]
|
|
RifleSkin.pulse(if rig then rig.flames else nil)
|
|
local tool = character:FindFirstChild(TOOL_NAME)
|
|
local handle = tool and tool:FindFirstChild("Handle")
|
|
if handle and handle:IsA("BasePart") then
|
|
local muzzle = handle.CFrame * CFrame.new(point("TPMuzzle"))
|
|
SniperEffects.flash(muzzle, attr("TPFlashSize"), workspace)
|
|
-- hitPosition is where the bullet stopped (the end of its range when it hit nothing)
|
|
if typeof(hitPosition) == "Vector3" then
|
|
SniperEffects.tracer(muzzle.Position, hitPosition)
|
|
end
|
|
end
|
|
end)
|
|
|
|
local function removeRig(character: Model)
|
|
local rig = rigs[character]
|
|
if rig then
|
|
rigs[character] = nil
|
|
if rig.skinChanged then rig.skinChanged:Disconnect() end
|
|
RifleSkin.destroy(rig.flames)
|
|
for _, inst in rig.extra do
|
|
inst:Destroy()
|
|
end
|
|
end
|
|
end
|
|
|
|
local function update()
|
|
local seen: { [Model]: boolean } = {}
|
|
for _, p in Players:GetPlayers() do
|
|
local character = p.Character
|
|
local tool = character and character:FindFirstChild(TOOL_NAME)
|
|
local handle = tool and tool:FindFirstChild("Handle")
|
|
local torso = character and (character:FindFirstChild("UpperTorso") or character:FindFirstChild("Torso"))
|
|
if character and tool and tool:IsA("Tool") and handle and handle:IsA("BasePart") and torso and torso:IsA("BasePart") then
|
|
seen[character] = true
|
|
local rig = rigs[character]
|
|
if rig and rig.handle ~= handle then
|
|
removeRig(character)
|
|
rig = nil
|
|
end
|
|
if not rig then
|
|
rig = makeRig(character, tool, handle, torso)
|
|
rigs[character] = rig
|
|
end
|
|
if rig.flames then
|
|
local own = p == player
|
|
local camera = workspace.CurrentCamera
|
|
local nearby = camera ~= nil and (camera.CFrame.Position - handle.Position).Magnitude < 100
|
|
local showFlames = (not own or config:GetAttribute("ThirdPersonView") == true)
|
|
and p:GetAttribute("InArena") == true and nearby
|
|
local humanoid = character:FindFirstChildOfClass("Humanoid")
|
|
RifleSkin.setMode(rig.flames, if showFlames and humanoid and humanoid.Health > 0 then "Full" else "Hidden")
|
|
RifleSkin.animate(rig.flames, os.clock())
|
|
end
|
|
-- live tuning: show the rifle offset at once (the server applies it to everyone)
|
|
local weld = torso:FindFirstChild(WELD_NAME)
|
|
if weld and weld:IsA("Weld") then
|
|
local k = kick(character)
|
|
weld.C0 = pose("TPGun")
|
|
* CFrame.new(0, 0, attr("TPRecoilBack") * k)
|
|
* CFrame.Angles(math.rad(attr("TPRecoilPitchDeg")) * k, 0, 0)
|
|
end
|
|
local u = reloadU(character)
|
|
for _, arm in (rig :: Rig).arms do
|
|
arm.target.CFrame = handPose(arm.key, u)
|
|
arm.pole.Position = point(arm.key .. "Elbow")
|
|
end
|
|
end
|
|
end
|
|
for character in rigs do
|
|
if not seen[character] then
|
|
removeRig(character)
|
|
reloadStarts[character] = nil
|
|
shotStarts[character] = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
RunService.PreSimulation:Connect(update)
|
|
|
|
-- ---------------------------------------------------------------- local third-person view (tuning)
|
|
local function applyView()
|
|
if config:GetAttribute("ThirdPersonView") == true then
|
|
player.CameraMode = Enum.CameraMode.Classic
|
|
player.CameraMaxZoomDistance = 128
|
|
player.CameraMinZoomDistance = 8 -- pushes the camera out of the head
|
|
task.delay(0.2, function()
|
|
if config:GetAttribute("ThirdPersonView") == true then
|
|
player.CameraMinZoomDistance = 3 -- allow zooming in closer afterwards
|
|
end
|
|
end)
|
|
else
|
|
player.CameraMinZoomDistance = 0.5
|
|
player.CameraMode = Enum.CameraMode.LockFirstPerson
|
|
end
|
|
end
|
|
config:GetAttributeChangedSignal("ThirdPersonView"):Connect(applyView)
|