Update
This commit is contained in:
commit
2d6a8a867d
1093 files changed
+2424571
No files matched your search
@@ -0,0 +1,192 @@
|
||||
-- Footsteps (LocalScript, StarterPlayerScripts): character sounds + snow footsteps.
|
||||
--
|
||||
-- 1. Own character: its 3D sounds (RbxCharacterSounds: Running, Jumping, Landing, FreeFalling, ...) were almost
|
||||
-- not heard while the camera follows the player. So every one of them is mirrored into a flat (not 3D) copy in
|
||||
-- SoundService.OwnCharacterSounds that follows the original exactly (play / stop / restart / speed / volume);
|
||||
-- the 3D originals are silenced through a SoundGroup with volume 0. Timing and sound stay the default ones.
|
||||
-- 2. Snow: on Snow / Glacier the normal footsteps are silenced and "snow step 1" (SoundService.Footsteps) plays,
|
||||
-- one sound per step. Own character: flat sounds; other players: 3D sounds in their root part.
|
||||
-- The ground is found with a short raycast down from the root part, skipping invisible parts (the zones have an
|
||||
-- invisible Plastic "Plot" part lying on the snow that the character physically stands on).
|
||||
local Players = game:GetService("Players")
|
||||
local RunService = game:GetService("RunService")
|
||||
local SoundService = game:GetService("SoundService")
|
||||
|
||||
local folder = SoundService:WaitForChild("Footsteps", 10)
|
||||
local snowTemplate = folder and folder:FindFirstChild("snow step 1")
|
||||
|
||||
local SNOW = { [Enum.Material.Snow] = true, [Enum.Material.Glacier] = true }
|
||||
local STRIDE = 5.3 -- studs per step: at the default 16 walk speed one step every ~0.33 s (walk animation)
|
||||
local MIN_SPEED = 1.5 -- slower than this = standing
|
||||
local POOL = 3 -- snow step copies played in turn, so a long step sound is not cut by the next one
|
||||
local OWN_GAIN = 0.85 -- flat copies are a bit quieter than the 3D original next to the ear
|
||||
-- snow step loudness = the Volume of SoundService.Footsteps["snow step 1"] (change it there, in Properties);
|
||||
-- other players' steps are 3D and fade with distance, so they get a little more
|
||||
local OTHER_SNOW_GAIN = 1.15
|
||||
|
||||
local localPlayer = Players.LocalPlayer
|
||||
local rays = RaycastParams.new()
|
||||
rays.FilterType = Enum.RaycastFilterType.Exclude
|
||||
rays.IgnoreWater = true
|
||||
|
||||
-- the 3D originals of the own character go into this silent group
|
||||
local muted = SoundService:FindFirstChild("MutedCharacterSounds") or Instance.new("SoundGroup")
|
||||
muted.Name = "MutedCharacterSounds"
|
||||
muted.Volume = 0
|
||||
muted.Parent = SoundService
|
||||
|
||||
local chars = {} -- [character] = state
|
||||
|
||||
---------------------------------------------------------------- own character: flat mirrors
|
||||
local function mirror(st, src, home)
|
||||
if not src:IsA("Sound") or st.mirrors[src] then return end
|
||||
local copy = Instance.new("Sound")
|
||||
copy.Name = src.Name
|
||||
copy.SoundId = src.SoundId
|
||||
copy.Parent = home
|
||||
local entry = { copy = copy }
|
||||
st.mirrors[src] = entry
|
||||
local function gain()
|
||||
if src.Name == "Running" and st.onSnow then return 0 end -- snow steps play instead
|
||||
return OWN_GAIN
|
||||
end
|
||||
entry.sync = function()
|
||||
copy.Volume = src.Volume * gain()
|
||||
copy.PlaybackSpeed = src.PlaybackSpeed
|
||||
copy.Looped = src.Looped
|
||||
end
|
||||
entry.sync()
|
||||
src.SoundGroup = muted
|
||||
local cons = st.cons
|
||||
table.insert(cons, src:GetPropertyChangedSignal("Volume"):Connect(entry.sync))
|
||||
table.insert(cons, src:GetPropertyChangedSignal("PlaybackSpeed"):Connect(entry.sync))
|
||||
table.insert(cons, src:GetPropertyChangedSignal("Looped"):Connect(entry.sync))
|
||||
table.insert(cons, src:GetPropertyChangedSignal("SoundId"):Connect(function() copy.SoundId = src.SoundId end))
|
||||
table.insert(cons, src:GetPropertyChangedSignal("SoundGroup"):Connect(function()
|
||||
if src.SoundGroup ~= muted then src.SoundGroup = muted end
|
||||
end))
|
||||
table.insert(cons, src:GetPropertyChangedSignal("Playing"):Connect(function()
|
||||
if src.Playing then
|
||||
copy.TimePosition = src.TimePosition
|
||||
copy.Playing = true
|
||||
else
|
||||
copy.Playing = false
|
||||
end
|
||||
end))
|
||||
table.insert(cons, src.Played:Connect(function() -- :Play() on a playing sound = restart
|
||||
copy.TimePosition = 0
|
||||
if not copy.Playing then copy.Playing = true end
|
||||
end))
|
||||
if src.Playing then copy.TimePosition = src.TimePosition; copy.Playing = true end
|
||||
end
|
||||
|
||||
---------------------------------------------------------------- setup per character
|
||||
local function setup(character)
|
||||
local hrp = character:WaitForChild("HumanoidRootPart", 10)
|
||||
local hum = character:WaitForChild("Humanoid", 10)
|
||||
if not hrp or not hum or chars[character] then return end
|
||||
local own = Players:GetPlayerFromCharacter(character) == localPlayer
|
||||
local st = { hrp = hrp, hum = hum, own = own, mirrors = {}, cons = {}, snow = {}, next = 1, dist = STRIDE * 0.6, onSnow = false }
|
||||
chars[character] = st
|
||||
|
||||
local home = hrp
|
||||
if own then
|
||||
home = SoundService:FindFirstChild("OwnCharacterSounds") or Instance.new("Folder")
|
||||
home.Name = "OwnCharacterSounds"
|
||||
home:ClearAllChildren() -- sounds of the previous life
|
||||
home.Parent = SoundService
|
||||
st.home = home
|
||||
hrp:WaitForChild("Running", 5) -- RbxCharacterSounds creates them
|
||||
for _, s in hrp:GetChildren() do mirror(st, s, home) end
|
||||
table.insert(st.cons, hrp.ChildAdded:Connect(function(s) mirror(st, s, home) end))
|
||||
else
|
||||
st.running = hrp:WaitForChild("Running", 5)
|
||||
st.runVolume = st.running and st.running.Volume or 0.65
|
||||
end
|
||||
|
||||
if snowTemplate then
|
||||
for i = 1, POOL do
|
||||
local s = snowTemplate:Clone()
|
||||
s.Name = "StepSnow" .. i
|
||||
s.Looped = false
|
||||
s.Volume = snowTemplate.Volume * (own and 1 or OTHER_SNOW_GAIN)
|
||||
s.RollOffMode = Enum.RollOffMode.Inverse -- (3D for other players) like the default sounds
|
||||
s.RollOffMinDistance, s.RollOffMaxDistance = 5, 150
|
||||
s.Parent = home
|
||||
st.snow[i] = s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function drop(character)
|
||||
local st = chars[character]
|
||||
chars[character] = nil
|
||||
if not st then return end
|
||||
for _, c in st.cons do c:Disconnect() end
|
||||
if st.own and st.home then
|
||||
for _, e in st.mirrors do e.copy:Destroy() end
|
||||
for _, s in st.snow do s:Destroy() end
|
||||
end
|
||||
end
|
||||
|
||||
local function watch(player)
|
||||
if player.Character then task.spawn(setup, player.Character) end
|
||||
player.CharacterAdded:Connect(function(c) task.spawn(setup, c) end)
|
||||
player.CharacterRemoving:Connect(drop)
|
||||
end
|
||||
for _, p in Players:GetPlayers() do watch(p) end
|
||||
Players.PlayerAdded:Connect(watch)
|
||||
|
||||
---------------------------------------------------------------- snow
|
||||
local function ground(st, character)
|
||||
if st.own and st.hum.FloorMaterial == Enum.Material.Air then return nil end
|
||||
local ignore = { character }
|
||||
local origin = st.hrp.Position
|
||||
local depth = st.hum.HipHeight + st.hrp.Size.Y / 2 + 2.5
|
||||
for _ = 1, 4 do
|
||||
rays.FilterDescendantsInstances = ignore
|
||||
local hit = workspace:Raycast(origin, Vector3.new(0, -depth, 0), rays)
|
||||
if not hit then return nil end
|
||||
local inst = hit.Instance
|
||||
if inst:IsA("Terrain") or inst.Transparency < 0.95 then return hit.Material end
|
||||
table.insert(ignore, inst)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function setSnow(st, onSnow)
|
||||
if st.onSnow == onSnow then return end
|
||||
st.onSnow = onSnow
|
||||
if st.own then
|
||||
for src, e in st.mirrors do if src.Name == "Running" then e.sync() end end
|
||||
elseif st.running and st.running.Parent then
|
||||
st.running.Volume = onSnow and 0 or st.runVolume
|
||||
end
|
||||
end
|
||||
|
||||
RunService.Heartbeat:Connect(function(dt)
|
||||
for character, st in chars do
|
||||
if not character.Parent or not st.hrp.Parent then
|
||||
drop(character)
|
||||
continue
|
||||
end
|
||||
local v = st.hrp.AssemblyLinearVelocity
|
||||
local speed = Vector3.new(v.X, 0, v.Z).Magnitude
|
||||
local mat = ground(st, character)
|
||||
if mat ~= nil then setSnow(st, SNOW[mat] == true) end -- in the air: keep the last ground
|
||||
|
||||
if st.onSnow and mat ~= nil and speed > MIN_SPEED and st.hum.Health > 0 and #st.snow > 0 then
|
||||
st.dist += speed * dt
|
||||
if st.dist >= STRIDE then
|
||||
st.dist = 0
|
||||
local s = st.snow[st.next]
|
||||
st.next = st.next % #st.snow + 1
|
||||
s.PlaybackSpeed = 0.92 + math.random() * 0.16 -- small variation, so the steps do not sound copy-pasted
|
||||
s.TimePosition = 0
|
||||
s:Play()
|
||||
end
|
||||
else
|
||||
st.dist = STRIDE * 0.6 -- the first step comes quickly
|
||||
end
|
||||
end
|
||||
end)
|
||||
Reference in new issue
Block a user