#sound playing on crash.
49 messages · Page 1 of 1 (latest)
** You are now Level 5! **
I cant give edit
ok
why not use .Touched instead of trying to calculate a sudden stop based on velocity? If you want to keep your velocity method, i think your logic for hasNegativeImpact could be wrong, or quite possibly how you're not using velocityChange at all.
lete me see
okay lets say i use .Touched but it doesn't solve my problem at all. as it will still be triggered as it hits the ground
** You are now Level 2! **
this should fix it local character = script.Parent
local RunService = game:GetService("RunService")
local humanoidRootPart = character:WaitForChild("Head")
local humanoid = character:WaitForChild("Humanoid")
local sound = character:FindFirstChild("ImpactSound") or Instance.new("Sound")
sound.Name = "ImpactSound"
sound.SoundId = "rbxassetid://8595980577"
sound.Volume = 1
sound.Looped = false
sound.Parent = character
local lastVelocity = Vector3.new()
local wallImpactThreshold = 20
local lastPlayTime = 0
RunService.Heartbeat:Connect(function()
local velocity = humanoidRootPart.Velocity
local velocityChange = (velocity - lastVelocity).Magnitude
local hasSignificantHorizontalImpact = (lastVelocity.X > 0 and velocity.X < 0) or
(lastVelocity.X < 0 and velocity.X > 0) or
(lastVelocity.Z > 0 and velocity.Z < 0) or
(lastVelocity.Z < 0 and velocity.Z > 0)
-- Ignore impacts where the velocity change is mostly vertical (Y-axis)
local horizontalChange = Vector3.new(velocity.X - lastVelocity.X, 0, velocity.Z - lastVelocity.Z).Magnitude
local verticalChange = math.abs(velocity.Y - lastVelocity.Y)
if horizontalChange > verticalChange and velocityChange > wallImpactThreshold and hasSignificantHorizontalImpact and tick() - lastPlayTime > 0.5 then
sound:Play()
lastPlayTime = tick()
end
lastVelocity = velocity
end)
i edit it for you in my studio
for auto correct lol
did you just use chatgpt
you didnt even put in a character variable
no need
tag any part that is considered "ground". Another way is to act on the direction between the 2 parts (so that hitting a roof will play the sound, but hitting a floor will not). I can't remember if roblox gives this with the touched event, but you can easily just use a raycast straight down from the player
I could but i wanted to make a automated thing
and also
the exposed parts
the edge here
if i crash into it
it should make the sound
but if i use tag and consider that part a ground
hitting the edge wont make the sound
pretty good edge case. details
?
suprisingly @shadow rock
your code does seem to work well
no let me see what you did with raycast
-- / Pre fabs PluginDecember 15, 2024 3:40 AM \ --
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RunService = game:GetService('RunService')
local ServerScriptService = game:GetService('ServerScriptService')
local Players = game:GetService('Players')
local character = script.Parent
local RunService = game:GetService("RunService")
local humanoidRootPart = character:WaitForChild("Head")
local humanoid = character:WaitForChild("Humanoid")
local sound = character:FindFirstChild("ImpactSound") or Instance.new("Sound")
sound.Name = "ImpactSound"
sound.SoundId = "rbxassetid://8595980577"
sound.Volume = 1
sound.Looped = false
sound.Parent = character
local lastVelocity = Vector3.new()
local wallImpactThreshold = 20
local lastPlayTime = 0
local function isWall(hit)
if not hit then return false end
local normal = hit.Normal
return math.abs(normal.Y) < 0.5
end
RunService.Heartbeat:Connect(function()
local velocity = humanoidRootPart.Velocity
local velocityChange = (velocity - lastVelocity).Magnitude
if velocityChange > wallImpactThreshold and tick() - lastPlayTime > 0.5 then
-- Perform a raycast in the direction of movement
local rayOrigin = humanoidRootPart.Position
local rayDirection = velocity.Unit * 2
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult and isWall(raycastResult) then
sound:Play()
lastPlayTime = tick()
end
end
lastVelocity = velocity
end)
here
idk if it works
yeah that kinda leaves you with the velocity calculation. another way still is to use touched and use a raycast to decide if the surface being touched is facing a certain direction, namely vector3.up
cant rlly test tell me if it works if you want to
yeah thats what i wanted to see
also i made a plugin it can be very usefull if you wanna see it
wdym suprisingly
what does it do