Trying to detect when a laser hits an object

Greetings! So lately I’ve been trying to develop a laser, where it fires a laser at your mouse and increases in size and so fourth. When it hits an object, it’ll then explode at that position.

I’ve been having issues with this however, I’m currently using raycasting to determine if the laser is about to hit an object, and it’s been very inconsistent. Sometimes the laser will explode when I fire it at a wall, sometimes it’ll just go right through it. Any suggestions appreciated! Here’s my code:

for i = 0,laser_range,1 do
	laser.Size = Vector3.new(5 * i,(charge_ball.Size.X),(charge_ball.Size.X))
	laser.CFrame = (CFrame.new(charge_ball.CFrame.p,base_hit) * CFrame.new(0,0,-laser.Size.X/2)) * CFrame.Angles(0,math.rad(90),0)
	local orient = (laser.CFrame * CFrame.new(laser.Size.X/2,0,0)).p
	local ray = Ray.new(orient,laser.CFrame.rightVector * 3)
	local hit,p = game.Workspace:FindPartOnRayWithIgnoreList(ray,{holder,char})
	if hit and i > 5 then
		local explosion = create_sphere()
		explosion.Size = laser.Size/2
		for i = 1,10,.25 do
			explosion.Size = explosion.Size + Vector3.new(.2,.2,.2)
			explosion.CFrame = laser.CFrame * CFrame.new((laser.Size.X/2),0,0)
			explosion.Transparency = (i / 10)
			game["Run Service"].RenderStepped:Wait()
		end
		break
	end
	game["Run Service"].RenderStepped:Wait()
end
1 Like

You create a ray with a fixed length?

local ray = Ray.new(orient,laser.CFrame.rightVector * 3)
2 Likes

Im not sure if thats the case, but i think that the “Laser” part that is created before your Raycast is not included in the ignore list and that Ray is sometimes hitting the Laser before it can hit anything else.

2 Likes

Thanks for the replies, guys.

@NexusKorthos

I believe all the parts of the laser are included in the model “holder”, which is inside of the ignore table.

@kingdom5

I’ve been messing around with the lengths of the ray, and I really haven’t noticed a huge difference.

The issue is just, sometimes the laser will keep on going, and the ray will never hit parts it clearly hits. I might need to investigate other methods to detect when the laser hits, but not really sure what those might be…any additional help here appreciated.

Is ‘holder’ a descendant of the character?

If so I’ve previously had issue when I’ve included two items in an ignore list, where one is a descendant of the other.

Also, consider just casting the ray first, then rendering the laser. That’s typically the standard for most things.

1 Like

Hm, I don’t believe so. Although I’ll double-check to make sure.

Assuming that isn’t the issue here, any other methods I could try? I’d think using the Touched event would provide inconsistent results as well, as I’ve noticed depending on the circumstance, it almost skips over what should’ve been called in to the connected function, so not sure if that’d be the way to go here.

Thanks!

I see you are trying to make a ray-cast gun. I’ve personally used many different methods of detecting when a ray hits a player. I’ve used touched events, ran loops to get hitting parts, and a variety of other terrible methods that I don’t care to state. Now, assuming there is no issue with your ray, this may be able to help you damage things or detect if a laser hits something.

That link also tells you one method to properly make a ray casting laser.

1 Like

Thanks! Although, I’m not making exactly that, it’s actually quite a bit different. (Not a tool as well.)

Any other ideas? Still am struggling a bit on this. I guess the issue is that the laser is quite large usually, so when I cast a ray it goes through the center of it. This might be the issue.

maybe you could try Region3 and this like

function CreateRegion3(part)
  local position, size = part.Position, part.Size
  return Region3.new(position-(size/2), position+(size/2)
end

function isTouching(laser)
 local Region = CreateRegion3(laser)
 local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(Region, ignoreList)

 for i, part in pairs(partsInRegion) do
   return part
 end
end

Sorry if I made a mistake I did this on mobile

I’d also check out this boxcasting discussion

1 Like

Have you tried testing with keeping the laser’s size consistent all the way through?

1 Like

Good idea. I mean, I could always cast two other rays to be oriented on the left and right sides of the laser. That way, pretty much all of the radius of the laser would be covered. I might actually try that and see how it works.

Maybe try converting the laser into a mesh/giving it a mesh and changing it’s properties so you don’t have to rely on changing the physical properties consistently. Maybe that’ll help with the raycasting?

1 Like

Hm, that’s an idea. Although, if the mesh is bigger than the actual part itself, that’ll cause more inconsistency issues. (The laser is big enough to hit a part, but it just goes right through it, for example.)

Thanks though!

I’d definitely recommend converting the laser in a mesh, and then modifying the properties. Give it a shot!

2 Likes