Detecting a beam touch!

Hiya everyone!

This is just a quick question, but is there any way to detect when a part is touching a beam? The things that look like this:

image

I’ve seen people saying they’re detecting it, but I’ve tried the touch method and raycasting, and none of them are working!

I’ve never used beams before, so I’m not sure if it’s possible or not. I looked on the wiki for raycasting, and it says a part or terrain, so I didn’t expect it to be possible, but in other forums people have mentioned themselves detecting beams with raycasting, and I’m a bit confused :sweat_smile:.

Any responses would be extremely helpful, thank you! (I’m a bit of a noob at raycasting/beams, I’ve never had the need for them before!)

1 Like

Were they detecting the beam itself, or just sending a Beam and a Ray at the same time and using the Raycast position to ‘detect’ where the beam was hitting?

1 Like

You cast a ray, with two vector points, (Start to end) which are the start and end points of the beam, The length of the ray should also be the magnitude between the two vectors.

Ray.new(part1.Position, Part2.Position)

Then you check for the part’s movement

Part:GetPropertyChangedSignal(“Position”):Connect(function()

end)

Then you use the “FindPartOnRay” function and you will get this:

function OnBeamHit()
--do something
end

Part:GetPropertyChangedSignal("Position"):Connect(function()
local BeamRay = Ray.new(part1.Position, Part2.Position)
local Part, Pos = workspace:FindPartOnRay(BeamRay)
end)

That’s the basic idea, that code is most likely wrong

1 Like

Beams are non-physical. You need to create your own form of collision/check here. Usually developers will opt to either use raycasts or attempt to line parts along the beam to serve as physics proxies. That being said, you said you’ve tried these methods but they didn’t work, so could you post those attempts? When asking questions, it’s helpful to include your past attempts so we can better help you.

3 Likes

Hiya, apologies for the late response. This is a representation of what I’m trying to do:

image

From a script inside of the part, I want to fire a ray cast downwards and detect when this ray cast hits the beam, so that I can get the exact y-value of the beam. (The beam is slanted and moving, if that has an effect). The diagram does kind of misrepresent it in a 3D world, the ray cast will be fired directly above the part and fired down (with the parts parent’s descendants blacklisted), and I want to detect the y value of the beam at the parts exact position.

This is what I’ve tried:

while wait() do
		local rayOrigin = shipController.Position + Vector3.new(20,20,0)
		local rayDirection = Vector3.new(0,-100,0)
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {shipController.Parent}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local ray = Ray.new(rayOrigin, rayDirection)
		local raycastResult = workspace:FindPartOnRayWithIgnoreList(ray, {shipController.Parent})
		
		local hitPart = raycastResult.Position
		print(hitPart)
	end

And also I tried:

	while wait() do
		local rayOrigin = shipController.Position + Vector3.new(20,20,0)
		local rayDirection = Vector3.new(0,-100,0)
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {shipController.Parent}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		
		local hitPart = raycastResult.Position
		print(hitPart)
	end

Both output nil.

Both are similar, just using different raycasting. The ray isnt directly above, but when it is, it still outputs nil as the raycastResult.

I’ve also just tried a simple touch function, but that doesnt output the beam (I didnt expect it too).

So is it possible to find a beam on ray casts? Similar to what I’m saying. I never thought it was, but it’s the fact I see people talking about detecting beams on ray casts.

And again, sorry for the late response!

Thank you!

EDIT
The beam is constantly there, attached between two attachments in two different parts, and these two parts are moving up and down horizontally. The two parts begin on the same y coordinate, and move up and down pretty much in sync (one moves up, one moves down, then they switch so the one going down starts going up, it’s simulating a wave) with each other! Sorry about the lack of transparency with what I’m trying to achieve.

The beam is there constantly, and is between two attachments on two parts (Further information below, I didnt explain it very well above :sweat_smile:).

Thanks!