How to tell when a moving unanchored part hits a wall?

Hey so I am making a grenade essentially in my game. The way it works is that its an unanchored part with a bodyvelocity that gets destroyed after 0.25 seconds to shoot it in the direction its being thrown. However I want to be able to play a sound everytime it hits a wall or bounces on the floor. How can I find out when the grenade hits a wall or bounces off the floor? Any help would be greatly appreciated!

local c c=grenade.Touched:Connect(function(h)if h:CanCollideWith(grenade) then sound:Play()c:Disconnect() end end)

1 Like

Oh I didn’t even know CanCollideWIth was a function lol. I’ll try this out and get back to you!

1 Like

One issue with using .Touched
It’s constantly firing the event as the grenade rolls on the floor and fires more than once when it hits a wall. (I don’t want to disconnect the touched event as I want it to be able to play multiple times).

I did do something similar with shell casings that come from guns playing sound when they hit the floor. I simply waited until the AssemblyLinearVelocity.Y value was 0. I’m assuming I can do something similar here but I’m not 100% sure how to go about it.

Try this:

local debounce
local c
c=grenade.Touched:Connect(function(h)
if debounce then return end
if h.Parent and grenade.Parent and h:CanCollideWith(grenade) then
sound:Play()
debounce = true
sound.Ended:Wait()
debounce = false
end
end)
1 Like

That solves it firing twice when it hits a wall but it also constantly fires the event as its rolling on the floor. Unfortunately I don’t think .Touched is gonna be the solution.

Alright. This should help:

local debounce
local c
c=grenade.Touched:Connect(function(h)
if debounce then return end
if h.Parent and grenade.Parent and h:CanCollideWith(grenade) and math.abs(grenade.AssemblyLinearVelocity.Y)>5 then
sound:Play()
debounce = true
sound.Ended:Wait()
debounce = false
end
end)

I just found a solution. I appreciate the help you’ve offered.

local lastvel = plantbomb.Sphere.AssemblyLinearVelocity
			
			repeat
				
				task.wait(.1)
				
				local vel = plantbomb.Sphere.AssemblyLinearVelocity
				
				if (vel-lastvel).Magnitude >= 30 then
					print("HIT")
				end
				
				lastvel = vel
			until not plantbomb:IsDescendantOf(game.Workspace)

Obviously this is just a test loop which I will optimize later but this does the trick. Only downside is that it prints (“HIT”) when its first thrown (which u can just make it ignore the first check to solve)

this makes much more sense! I also recommend switching BodyVelocity to LinearVelocity since BodyVelocity is deprecated.

Also, if this is done on the server try SetNetworkOwner() without any parameters.

1 Like

It’s similar to if Part1.CollisionGroupId == Part2.CollisionGroupId then.

1 Like