Weird raycast collision problem

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a sound manager based on playing music/ambience assigned to certain areas of the game using parts and raycasts.

  2. What is the issue? Include screenshots / videos if possible!
    The parts used to label areas for designated music/ambience have weird collissions, it seems as if the collisions for parts are closer to the 0,0,0 position meanwhile the parts themselves visually stay in the same place as before the playtesting.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to change the origin of the raycast from player’s root part position to player’s torso position then to the current camera’s position, nothing affected the issue.

-- snippet of the raycast code part itself in the local script in StarterPlayerScripts
while task.wait(.1) do
	local og = workspace.CurrentCamera.CFrame.Position
	local dir = og - Vector3.new(0,30,0)
	
	local R1 = workspace:Raycast(og, dir, RCP1)
	if R1 and R1.Instance then
		local amb = R1.Instance:FindFirstChildOfClass("Sound")
		local folder = R1.Instance:FindFirstChildOfClass("Folder")
		if not folder or #folder:GetChildren() == 0 then
			SFX_Folder = nil
		else
			SFX_Folder = folder
		end
		if CA.SoundId ~= amb.SoundId then
			changeAmbient(amb)
		end
	else
		changeAmbient(script.Sound)
	end
	
	local R2 = workspace:Raycast(og, dir, RCP2)
	if R2 and R2.Instance then
		local song = R2.Instance:FindFirstChildOfClass("Sound")
		if CS.SoundId ~= song.SoundId then
			changeMusic(song)
		end
	else
		changeMusic(script.Sound)
	end
end

I’ll provide videos presenting the issue if needed, thank you for any of the answers

1 Like

Replying to this issue due to no responses

Provide a video bc it’s sorta confusing now. You want to play a different sound everytime a player is in a different area marked by a different part?

wouldn’t it just be simpler to use .Touched and .TouchEnded to accomplish this??

Touched is not very reliable when it comes to playing ambiences as the sound needs to be played for a prolonged amount of time


The yellow area is where music is assigned, meanwhile the blue area is where the ambience is assigned. The spawnpoint is almost exactly at position 0,0,0


it appears that collisions of parts which are assigned the music/ambience are closer to the spawn (0,0,0 position) than the parts themselves

Okay i found the solution, i just had to replace local dir = og - Vector3.new(0,30,0) with local dir = (hrp.CFrame.UpVector*20)*-1

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.