How to Stop Parts from Clipping Through Walls

e1e9dd56e6beee7b9c4c626abcbef9c30e48f7ce
I want to shorten parts from only one face, The gif above shows my current method.

WHAT I’VE TRIED:
Moving the Cframe after changing Size property

my code:

local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local tweenserv = game:GetService("TweenService")
local blade = script.Parent.Blade 
local tipTopAttachment = blade:WaitForChild("TIPTOP")  
local bottomAttachment = blade:WaitForChild("BOTTOM")
local deltsize = 9

local beam = Instance.new("Beam")
beam.Name = "LaserBeam"
beam.Color = ColorSequence.new(Color3.fromRGB(255, 0, 0))  
beam.TextureMode = Enum.TextureMode.Stretch
beam.Parent = blade 

beam.Attachment0 = bottomAttachment  
beam.Attachment1 = tipTopAttachment  

-- update
local TweenInfo = TweenInfo.new(0.01, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) --smooth 

RunService.Heartbeat:Connect(function()
	-- Calculate the direction from BOTTOM 
	local direction = (tipTopAttachment.WorldPosition - bottomAttachment.WorldPosition).Unit

	--  raycast from the BOTTOM attachment in the Z-axis direction (10 studs max)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true

	-- ignore parts
	local playerParts = {}
	for _, player in ipairs(Players:GetPlayers()) do
		local character = player.Character or player.CharacterAdded:Wait()
		for _, descendant in ipairs(character:GetDescendants()) do
			if descendant:IsA("BasePart") then
				table.insert(playerParts, descendant)
			end
		end
	end

	local ignoreParts = CollectionService:GetTagged("IGNORE")
	local finalIgnoreParts = {}
	for _, part in pairs(playerParts) do
		table.insert(finalIgnoreParts, part)
	end
	for _, part in pairs(ignoreParts) do
		table.insert(finalIgnoreParts, part)
	end
	raycastParams.FilterDescendantsInstances = finalIgnoreParts


	local raycastResult = workspace:Raycast(bottomAttachment.WorldPosition, direction * 10, raycastParams)

	
	local newBladeSizeZ
	if raycastResult then
		--getdistance from hitpoint to bottomattachment
		local distance = (raycastResult.Position - bottomAttachment.WorldPosition).Magnitude
		newBladeSizeZ = math.min(distance, 10) --  length to a max of 10 studs
	else
		--defaultcase
		newBladeSizeZ = 10
	end



	
	local targetProperties = {
		Size = Vector3.new(blade.Size.X, blade.Size.Y, newBladeSizeZ),
	}

	local tween = tweenserv:Create(blade, TweenInfo, targetProperties)
	tween:Play()
end)

The issue with changing the position is changing the cframe. the blade is supposed to follow your mouse, which changes the position frequently.

Changing the Cframe of the blade WHILE moving is really tedious, and I can’t seem to figure it out.

I was also suggested to use Part:Resize(), But had no luck with it.

If anyone has a solution or any improvements to my implementation, I am open to them.

Thank you!

1 Like

I have tried changing the position in relation to the NEW size, but no luck ‘

Well, why do you need to tween the blade after calculating? By the looks of it, you are tweening the size to adjust the visible parts of the blade, cutting out invisible segments that are intersecting with parts. Tweening, even with extremely short durations, DOES NOT GUARANTEE that the blade will be in the same place at point A/B

Why can this not be done by simply changing the size? Even then, you should keep in mind that changing the size of an object also changes its CFrame and Position values, because an object that occupies less space will not fit into the same space as a larger object, and resizing technically is a modified moving tool.

Also, keep in mind that when you change size alignment, the size is subtracted equally from the top and bottom. [THIS IS ONLY NOT TRUE IF YOU USE CONSTRAINTS, WHICH IN THAT CASE, THIS NEXT STEP WILL NOT WORK]

So, to counteract this, you should move the entire blades CFrame.p down by (unit decrease)/2. This will replace the blade at the same starting position, but appearing shorter.

If you’ve already tried this, feel free to let me know, but you did not provide your exact processes so far.

1 Like

Hey,
I had thought to use tween service to smoothen out the sizing, but not the position.
Thanks for letting me know that changing the size of an object also changes its CFrame and Position value. Do you have any suggestions as to how to get past this?

I also looked into part:resize(), as it shortens parts like the studio scale tool, but couldn’t figure out how to revert the size after it shrunk like 1,000 times, and it still looked like it was scaling from both sides.

Below is the hierarchy of my thing

  • Sword
    • Blade (blade)
    • Handle (Handle)
      • Realblade (real blade - big, allows for 2D fighting)

They are all welded together, with the handle being mounted to the hand which follows the cursor (See gif in original post)