Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

make raycast go in a direction infinitely?

So, I'm making a top-down shooter using raycast guns. Now, I know that rays can't be created with infinite lengths (the max distance is 512, I believe?) however I want to make it so that the ray goes straight forward in the direction of the mouse rather than ending at the mouses position. So, like, when you click, rather than the ray going down and ending at the mouse.Hit position, it keeps going straight past the mouse.Hit position and ending after 200 studs. So like the Y position of the mouse isn't taken into factor.

Here's the script for my gun:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
	print("equipped")
	mouse.Button1Down:connect(function()
		
		print("bang")
		
		script.Parent.Fire:Play()
		
		local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 200)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Medium stone grey")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.5
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (tool.Handle.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
		
		repeat
			wait(.05)
			beam.Transparency = beam.Transparency + .1
		until beam.Transparency == 0
		
		game:GetService("Debris"):AddItem(beam, 0.5)	
		
	end)
end)

The code is pretty basic; pretty much the same as any other simple raycast gun.

I'm not sure if what I'm asking is even possible, but I figured that I'd have better luck asking smarter people rather than spending headache-inducing hours trying to figure it out myself.

All help is appreciated!

2 comments

0

A ray is technically infinite the function FindPartOnRay has a magnitude limit.

0

I don't get what you mean zemen. Like above, rays don't "end at mouse position", FindPart does. Simply ignore some parts.

1 answer

I'm very very late but maybe raycast multiple times at the end on each ray so it's infinite?

This discussion is preserved as it was. New answers, comments, and votes are closed.