Help with part extending past another

I’m trying to prevent the part spawned by the ray from extending beyond the original part hit. As of now, the spawned parts are stretching outside past the original part, as seen in the first image. However, I am aiming to achieve what’s pictured in the second image

code:

local frozedparts = {}
local FreezeParts = {}
local module = require(game.ReplicatedStorage.lotsoffunstuff)
local ice = script.Ice:Clone()
local playersfrozen = {}
script.Ice:Destroy()

local function func(v, vec)
	if vec and v then
		local newpart = Instance.new("Part", workspace)
		newpart.Color = script.Parent.Color
		newpart.Name = "FreezePart"

		for i,v in pairs(script.icedecals:GetChildren()) do
			local clon = v:Clone()
			clon.Parent = newpart
		end
		newpart.Color = Color3.fromRGB(4, 175, 236)
		newpart.Material = Enum.Material.SmoothPlastic
		newpart.CanCollide = false
		newpart.Transparency = 0.7

		newpart.Touched:Connect(function(hit)
			local hum = module.FindHumanoid(hit.Parent)
			if hum then
				local root = hum.Parent:FindFirstChild("HumanoidRootPart")
				if root then
					if playersfrozen[hum.Parent] == nil then
						playersfrozen[hum.Parent] = true
						root.Anchored = true
						local icing = ice:Clone()
						table.insert(FreezeParts, icing)
						icing.Parent = workspace
						icing.CFrame = root.CFrame * CFrame.Angles(math.rad(-90),0,0)
						wait(2)
						root.Anchored = false
						icing:Destroy()
						playersfrozen[hum.Parent] = nil
					end
				end
			end
		end)

		local ray = RaycastParams.new()
		ray.IgnoreWater = true
		ray.FilterType = Enum.RaycastFilterType.Exclude
		ray.FilterDescendantsInstances = {script.Parent, FreezeParts, frozedparts}
		local realray = workspace:Raycast(script.Parent.Position, vec * 50, ray)

		if realray then
			local partthing = realray.Instance
			if partthing then
				local hum = module.FindHumanoid(partthing.Parent)
				if not hum then
					--if partthing.Locked == false then
					table.insert(frozedparts, partthing)
					newpart.CFrame = CFrame.new(realray.Position, realray.Position + realray.Normal)
					if (script.Parent.Position - newpart.Position).Magnitude <= 25 then
						table.insert(FreezeParts,newpart)
						newpart.Orientation = partthing.Orientation
						local SizeX = math.clamp(newpart.Size.X, partthing.Size.X - partthing.Size.X/2, partthing.Size.X + partthing.Size.X/2)
						local SizeZ = math.clamp(newpart.Size.Z, partthing.Size.Z - partthing.Size.Z/2, partthing.Size.Z + partthing.Size.Z/2)
						local SizeY = math.clamp(newpart.Size.Y, partthing.Size.Y - partthing.Size.Y/2, partthing.Size.Y + partthing.Size.Y/2)

						if partthing.Size.X <= 50 or partthing.Size.Z <= 50 or partthing.Size.Y <= 50 then
							if partthing.Size.X > 20 or partthing.Size.Y > 20 or partthing.Size.Z > 20 then
								newpart.Size = Vector3.new(SizeX, SizeY, SizeZ) * Vector3.new(0.8,0.8,0.8)
							else
								newpart.Size = Vector3.new(SizeX, SizeY, SizeZ)
							end 
						else
							newpart:Destroy()
						end
					else
						newpart:Destroy()
					end
					--	end
				else
					local rootpart = hum.Parent:FindFirstChild("HumanoidRootPart")	
					if rootpart then
						spawn(function()
							local icing = ice:Clone()
							table.insert(FreezeParts, icing)
							icing.Parent = workspace
							icing.CFrame = rootpart.CFrame * CFrame.Angles(math.rad(-90),0,0)
							rootpart.Anchored = true
							wait(2)
							rootpart.Anchored = false
							icing:Destroy()
						end)
					end
				end
			end
		end

		newpart.Anchored = true
	end
end
wait(9)
script.Parent.ice_shatter:Play()
for i,v in pairs(workspace:GetDescendants()) do
	if v then
		if v ~= script.Parent then
			if v:IsA("Part") then
				local magnitude = (script.Parent.Position - v.Position).Magnitude
				if magnitude <= 50 then
					func(v, script.Parent.CFrame.RightVector.Unit)
					func(v, -script.Parent.CFrame.RightVector.Unit)
					func(v, script.Parent.CFrame.UpVector.Unit)
					func(v, -script.Parent.CFrame.UpVector.Unit)
					func(v, script.Parent.CFrame.LookVector.Unit)
					func(v, -script.Parent.CFrame.LookVector.Unit)
				end
			end
		end
	end
end


1 Like

In general, for rectangles the equations are the following:

local px, py = bigPart.Position.X, bigPart.Position.Y
local dx = (smallPart.Size.X-bigPart.Size.X)/2
local dy = (smallPart.Size.Y-bigPart.Size.Y)/2
local minX, maxX, minY, maxY = px+dx, px-dx, py+dy, py-dy

local x = math.clamp(someX, minX, maxX)
local y = math.clamp(someY, minY, maxY)
print(x, y)

where bigPart is the large part acting as canvas, smallPart is the moving part, and someX and someY are the positional cframe coordinates(basically the two except the depth one), keep in mind you may have to change some coordinates(for example use z except y for size, etc.) if your parts aren’t properly rotated.

1 Like

For some reason, the ice part now just goes to the center of the part and I don’t know why

Mess with the positional and size coordinates until it works(for example swap y with z etc) basically rotations and such mess it up.

1 Like

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