CanCollide Moving Parts not Interacting with each other

In my game, I am trying to check and see if a laser is touching a mirror.

I have read online that people have come across this error before, and in fact, it doesn’t work.

I have seen a few answers, but some look deprecated.

How can I get around this? Here are some of my scripts:
Move script:

local Part = script.Parent

local move = script.Parent.move

game:GetService("RunService").Heartbeat:Connect(function()
	if move.Value then
		Part.Size = Vector3.new(Part.Size.X,Part.Size.Y,Part.Size.Z + 1)
		Part.CFrame = Part.CFrame * CFrame.new(0,0,-0.5)
		print('moving')
	end
	
	
end)

Here is what I have of the stop scripts:

local laser = script.Parent

laser.Touched:Connect(function(hit)
	
	if hit.Parent.Name == "SmokingMirror" then
		print("hi")
	else
		print(hit.Parent)
		
	end

end)
1 Like

What do the deprecated answers look like? You need to raycast to detect the mirror.

Would raycasting be a good method? I have never actually used raycast. Any good sources?

Not any other than the API documentation. I learned it myself.

You call Workspace:Raycast and provide an origin and direction, but also with RaycastParams. In your case, you can include the mirror inside the filtering mode.

Once completing the cast, you get the position. This can be used to resize the part and position it to your liking.

But how do I raycast from a laser? Since it’s moving would it be able to check? I feel like there might be a lack of precision with that.

No, you’d just have to raycast from the origin each frame or update it multiple times with a new cast result.

For this case raycast is your best option, it is cheap, fast and easy to use.

In order to use this method you need 3 parameters, the first one is the origin, a Vector3 that defines the start position of the laser; the second one is the direction of the laser (in the example code I will use the mouse hit position as the end position of the beam to calculate the location); and the third parameter is the RaycastParams used to fikter and configure certain aspects of your ray.

-- ...
local origin = Character.HumanoidRootPart.Position -- the origin position for the laser
local direction = (Mouse.Hit.Position - origin).Unit * 25 -- the distance of the ray will be 25 studs.
local rayparams = RaycastParams.new()
rayparms.FilterType = Enum.RaycastFilterType[...]
-- other configurations

local result = workspace:Raycast(origin, direction, rayparams)
-- ...

There might be issues with this code, am on my mobile phone, but it should work fine as reference

1 Like

OK! Thank you. Just a quick question, anyway to see where the raycast is going?

How would I use this to set the direction? In the direction part can I use CFrame?

Just using another method a developer friend sent me.

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