How can I launch a bullet with RayCasting?

Hello I’m trying to learn how to launch a bullet using RayCasting, I’ve tried using bodyVelocity but it didn’t look so great. I’ve been reading all over the developer.Roblox.com but I still don’t understand it.

This is the code for a lasergun

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
 
tool.Equipped:connect(function(mouse)
	print("Tool equipped!")
	
	mouse.Button1Down:connect(function()
		print("Mouse pressed!")
		local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		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)
		
		game:GetService("Debris"):AddItem(beam, 0.1)
	end)
end) 

The part I do not understand tho is:

		local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

and

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) 

Just a little explanation with help a bunch thanks!

The code here is creating a Ray via the Ray.new(Origin, Direction) constructor, in this example specifically it’s creating a Ray from the Tool’s Position in the direction of a normalised vector which is the difference between the Tool’s Position and the Position of where the Mouse is hovering over a Part multiplied by 300.

Imagine the direction provided here as an arrow, which starts at the Tool’s Position and is pointing towards the Mouse Position by 1 stud - this arrow is extended by 300 in size in the direction it’s facing.

This code is finding the first part on this Ray, while ignoring any descendants of the Player’s Character.

If it finds a part, it’ll return what part it’s hitting and the position it’s hitting it at.

Basically, here it’s finding the length (magnitude) between the difference of the Handle’s Position and where it’s hitting. This’ll be greatly useful for making a trail between where it’s fired and hit.

Directly, the Beam’s size is set to 0.3, 0.3 and the length between where it was fired and where it hit as the Z axis.

The beam is now positioned to be at where it was fired, facing where it hit with some added offset of the length between where it fired and hit divided by two - that is essentially the middle between the two.


If you wanted to use a BodyVelocity, you could just find the difference vector between the Tool’s Handle’s Position and the position of where the ray hit and then normalise it.

BodyVelocity.Velocity = (tool.Handle.Position - position).Unit

It’s possible to multiply the Unit vector by how many studs per second you want it to move

2 Likes