Puddle orientation

Ive been trying to make a script that orients the lava puddles to make them stick to the terrain but cant quite make it work.

This is what I tried:

smallMagma.Touched:Connect(function(hit)
			if not hit:FindFirstAncestor("Map") then return end
			smallMagma.Anchored = true
			
			local raycastParams = RaycastParams.new()
			raycastParams.FilterType = Enum.RaycastFilterType.Include
			raycastParams.FilterDescendantsInstances = {hit}
			local raycast = workspace:Raycast(smallMagma.Position + Vector3.new(0, 40, 0), Vector3.new(0, -100, 0), raycastParams)
			
			local origin
			if raycast then
				smallMagma.Position = raycast.Position
				smallMagma.MagmaLayer.Position = raycast.Position
				origin = raycast.Position
			else
				origin = smallMagma.Position
			end
			
			local locked = {}
			
			local function calculate(offset: CFrame, axis: string)
				local raycast = workspace:Raycast((smallMagma.CFrame * offset).Position + Vector3.new(0, 40, 0), Vector3.new(0, -100, 0), raycastParams)
				
				
				if raycast then
					local low = Vector3.new(origin.X, raycast.Position.Y, origin.Z)
					local hypothenuse = (origin - raycast.Position).Magnitude
					local opposite = (origin - low).Magnitude
					
					local angle = math.asin(opposite/hypothenuse)
					
					if angle ~= angle then return end
					if table.find(locked, axis) then return end
					
					print(angle)
					
					if axis:lower() == "x" then
						smallMagma.CFrame *= CFrame.Angles(0, 0, angle)
					elseif axis:lower() == "y" then
						smallMagma.CFrame *= CFrame.Angles(0, angle, 0)
					elseif axis:lower() == "z" then
						smallMagma.CFrame = CFrame.Angles(angle, 0, 0)
					end
					table.insert(locked, axis)
				end
			end
			
			TweenService:Create(smallMagma, TweenInfo.new(magmaSpeed / 2, Enum.EasingStyle.Sine), {Size = magmaSize * Vector3.new(0.5, 1, 0.5)}):Play()
			TweenService:Create(smallMagma.MagmaLayer, TweenInfo.new(magmaSpeed / 2, Enum.EasingStyle.Sine), {Size = magmaSize * Vector3.new(0.5, 1, 0.5) + Vector3.one * 0.2}):Play()
			
			calculate(CFrame.new(1, 0, 0), "x")
			calculate(CFrame.new(-1, 0, 0), "x")
			calculate(CFrame.new(0, 1, 0), "z")
			calculate(CFrame.new(0, -1, 0), "z")
		end)

This is what I get:

image

Ive also tried using cosines but it had the same effect. Also puddles sometimes get rotated by 90 degrees.

Anyone knows how to make it rotate properly?

This is what the Raycast returns

You want to use the Normal from that.

Thanks!

Ive been a scripter for 3+ years now and I didnt know about this somehow.

1 Like

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