Problem with Raycasting

Hello.
I’m currently having an issue with Raycasting.
Basically, I made a beam attack and made it so that the end of the beam is always at the ray’s collision with the other elements.
The problem is, the end of the beam keeps coming back and forward.


Script

The loop runs the code for about 2-3 seconds.

		for count = 0, 200, 1 do
			local rayOrigin = humanoidRootPart.Position
			local rayGoal = GetCFramePosition(humanoidRootPart.CFrame + humanoidRootPart.CFrame.LookVector * 150)
			local rayDirection = rayGoal - rayOrigin
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {character}
			raycastParams.FilterType = Enum.RaycastFilterType.Exclude
			raycastParams.IgnoreWater = true
			
			corruptedBeam:WaitForChild("Start").Position = rayOrigin
			corruptedBeam:WaitForChild("Start").Orientation = humanoidRootPart.Orientation
			corruptedBeam:WaitForChild("End").Position = raycastResult.Position
			corruptedBeam:WaitForChild("End").Orientation = humanoidRootPart.Orientation
			corruptedBeam:WaitForChild("Hitbox").Position = raycastResult.Position
			wait()
		end

Does anyone know how to make it so the end of the beam stays at the ray’s collision instead of going back?

1 Like

On a loop you are constantly Raycasting to find the Hit position, that makes sense, but the loop is changing the Beam End constantly too, following a new Raycast hit position that is created per iteration at the same time on each iteration

1 Like

Oh. So I should try puting the end part into the ignore list?

Ok well I just tried that and it worked.
But you were the one who put me in the right direction :wink:

Thanks a lot!

1 Like

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