Detect Two Blocks Touching

Hello!

I am making special abilities that damage parts. What I mean is imagine I have a lightning strike that I can use to hit the ground; when the lightning touches the ground, an event would happen. The problem is, since the bomb is anchored, Part.Touched will not work in this scenario. What can I use instead?

1 Like

You could use Raycasting. Just fire rays towards the ground to detect if they’re touching, but I’m not sure why .Touched wouldn’t work if the object is anchored, .Touched works either way

1 Like

How would I do that? (Not familiar with raycasting;)

BasePart.Touched documentation:

This event only fires as a result of physical movement, so it will not fire if the CFrame property was changed such that the part overlaps another part. This also means that at least one of the parts involved must not be Anchored at the time of the collision.

1 Like

How do you throw the part if it is anchored?

1 Like

I don’t necessarily throw the part, but edit the position to move it forward. I’ll fix the OP to be more clear.

1 Like

You could check use Bomb:GetTouchingParts(). It returns a list with each part intersecting your bomb.

1 Like

This works too, but in my experience Raycasting is more efficient.
If you want more information and example code, visit this page:

1 Like

How can I use Raycasting for it? I don’t really understand.

P.S this is what I mean

1 Like

For example, when you finish your animation, fire a ray towards the ground and detect if it collided with something.

local rayOrigin = Vector3.new(0, 0, 0) -- What position you want to shoot the ray from.
local rayDirection = Vector3.new(0, -50, 0) -- How far you want to raycast (X, Y, Z)

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

if raycastResult then
	print("Hit: " .. raycastResult.Instance)
end
1 Like

The part touches the ground, however, nothing is being printed in the output.

(pos is the position of the players mouse)

	local rayOrigin = Vector3.new(spiritbomb.Position) 
	local rayDirection = Vector3.new(pos)

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	
	for i = 1, 250, 1 do
		spiritbomb.CFrame *= CFrame.new(0, 0, -1.5)
		if raycastResult then
			print("Hit: " .. raycastResult.Instance)
		end
		task.wait(0.01)
	end
1 Like

I remember doing this a while ago.
This function is a real eyesore, so brace yourself

local function checkIfTouching(p1, p2)
	return (
			(p1.Position.X - p1.Size.X/2 <= p2.Position.X + p2.Size.X/2) and
			(p1.Position.X + p1.Size.X/2 >= p2.Position.X - p2.Size.X/2) and
			(p1.Position.Y - p1.Size.Y/2 <= p2.Position.Y + p2.Size.Y/2) and
			(p1.Position.Y + p1.Size.Y/2 >= p2.Position.Y - p2.Size.Y/2) and
			(p1.Position.Z - p1.Size.Z/2 <= p2.Position.Z + p2.Size.Z/2) and
			(p1.Position.Z + p1.Size.Z/2 >= p2.Position.Z - p2.Size.Z/2)
	)
end
1 Like

Probably start raycasting inside the loop.

For some reason, the output says unable to index nil with position when the part exists.

@Axmos Still doesn’t work, nothing is being printed even when the parts collide.