How can I make raycast go toward the look vector?

Hello,
I’m a beginner at raycasting and was wondering how can I make a raycast toward the look vector? Any help is appreciated since I’m new to raycast.
Thank you,

When you’re making a raycast, it takes 3 parameters. The origin of the ray, the direction it’s going in and any RaycastParams. A simple raycast would look like this, as you should know:

local ray = workspace:Raycast(part.Position, Vector3.new(100, 0, 0)) -- Raycast Params aren't necessary

If this hit’s anything it returns info about what it hit, etc. This however as you’d know only shoots 100 studs on the x axis, and that’s not useful 99.9% of the time. If you want to use the LookVector, it’s pretty simple. LookVectors already have normalized units of 1, so to cast, say 20 units ahead, it’d be the LookVector * 20, which is quite literally just that.

It should look something like this:

local ray = workspace:Raycast(part.Position, part.CFrame.LookVector * 20)

There is 1 issue I’d like to point out with this though. Since you’re casting the ray from the position of the part, you’ll end up hitting it because the Position is the centre. To fix this you can either add it to a blacklist in the ray or just add half the parts size to the position.

It doesn’t seem to be firing I know it’s not your problem mostly mine so how should I fix this?
Script:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player)
	local Direction = player.Character:WaitForChild("Direction")
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local startPos = player.Character.Direction.Position
	local range = 300 
	local aimDirection = Direction.CFrame.LookVector
	local rayResult = workspace:Raycast(startPos, aimDirection, raycastParams)

	if rayResult then
		local hitPart = rayResult.Instance
		local hitPos = rayResult.Position

		if hitPart then
				--hitPart.Parent.Humanoid:TakeDamage(30)
		end
		
		local laserPart = Instance.new("Part")
		laserPart.Anchored = true 
		laserPart.Name = "Laser"
		laserPart.BrickColor = BrickColor.new("Really red")
		laserPart.CanCollide = false
		laserPart.Size = Vector3.new(0.5, 0.5, Vector3.new(100,0,0))
		laserPart.CFrame = CFrame.new(Direction.Position, hitPos) * CFrame.new(1, 0, 1)
		laserPart.Parent = workspace

	--	wait(0.2)
	--	laserPart:Destroy()
	end
end)

Your ray is only 1 stud long. I suggest doing aimDirection * range.

Sorry for the late response, it has an error which is:
Unable to cast double to RaycastParams

Quoted from PMs:

You used range (which is a double) on the parameter that’s supposed to be RaycastParams. Do:

local rayResult = workspace:Raycast(startPos, aimDirection * range, raycastParams)

The last parameter isn’t supposed to be a number, it’s supposed to be parameters for the raycast. The correct way to do it is to multiply aimDirection by range in the second parameter (the correct one to put a Vector3 in).

3 Likes

You can do a little math to get a visual projectile:

local midPoint = (startPosition + aimDirection * range) / 2 -- Getting the center between the parts

local Part = Instance.new("Part")
Part.CFrame = CFrame.new(midPoint, aimDirection * range)
Part.Size = Vector3.new(1, 1, range)
Part.Parent = workspace

Was supposed to be a reply but it got deleted before I managed to post it.

1 Like