Stuttering Custom Collision System

So i am trying to make a moving tentacle system, and this is how i did the collision :

Script
local elapsed = 0
RunService.RenderStepped:Connect(function(dt)
	elapsed = elapsed+(dt)
	for i,tentacle in pairs(tentacleslist) do
		--// raycast check collision
		local origin = tentacle.CFrame.Position

		local IgnoreList = {tentacleslist}

		local OffsetCollide = CFrame.new(0,0,0)

		local overlapParams = OverlapParams.new()
		overlapParams.FilterDescendantsInstances = IgnoreList
		overlapParams.FilterType = Enum.RaycastFilterType.Exclude

		local RaycastParamParam = RaycastParams.new()
		RaycastParamParam.FilterDescendantsInstances = IgnoreList
		overlapParams.FilterType = Enum.RaycastFilterType.Exclude

		local parts = workspace:GetPartBoundsInBox(tentacle.CFrame,tentacle.Size, overlapParams)
		local collided = #parts > 0

		if not collided then

		else
			for i,v in ipairs(parts) do
				local position = (v.Position - origin)
				local direction = position.Unit * position.Magnitude
				local raycast = workspace:Raycast(origin, direction, RaycastParamParam)
				visualizeRay(origin,direction,true)

				if raycast then

					local normal = raycast.Normal
					OffsetCollide = OffsetCollide * CFrame.new(normal*0.6)
					print(OffsetCollide)
				end
			end
		end

		--------------	
                --// Where i apply the CFrames 
		local originalCF = tentacle.CFrame:Lerp(tentacle.Parent.CFrame * CFrame.new(math.noise(elapsed,i)*1.3,math.noise(elapsed,1/i)*1.3,2),1/(2*tentacle.Name)) 
		tentacle.CFrame = originalCF * OffsetCollide

	end
end)

How i make it is, For each part of the tentacle , it will check if its collided with something every renderstepped using the "GetPartBoundsInBox’’ function. Then there will be a raycast casted from the section of the tentacle to the collided part to check the normal. Then i apply the normal to the final CFrame of the section of the tentacle. But it stutters sooo bad

This is how the tentacles are generated:

Script
local tentacleslist = {}
local currenttentacle = HRP
for i = 1,5 do
	local tentacle = Instance.new("Part")
	tentacle.Name = tostring(i)
	tentacle.Size = Vector3.new(0.6,0.6,0.6)
	tentacle.Anchored = true
	tentacle.CanQuery = false
	tentacle.CanCollide = false
	tentacle.Parent = currenttentacle
	tentacle.CFrame = currenttentacle.CFrame * CFrame.new(0,0,0.7)
	currenttentacle = tentacle
	table.insert(tentacleslist,tentacle)
end

But, it stutters so bad…

External Media
1 Like