How would I visualize a raycast?

I just learned how to make myself a simple gun using a youtube tutorial, the only thing I need is to visualize the raycast. I just don’t know how to. How would I do that? Here’s that code that I did.

client:

local mouse = game.Players.LocalPlayer:GetMouse()

script.Parent.Activated:Connect(function()

script.Parent.Fire:FireServer(mouse.Hit.p)

end)

server:

script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)

script.Parent.Handle.Shoot:Play()

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local tip = script.Parent.Tip.Position
local raycastResult = workspace:Raycast(tip, (mousePos - tip)*300, raycastParams)

if raycastResult then
local hitPart = raycastResult.Instance
local model = hitPart:FindFirstAncestorOfClass(“Model”)

  if model then
  	if model:FindFirstChild("Humanoid") then
  		model.Humanoid.Health -= 30
  	end
  end

end
end)

Screenshot_212

-- for more info on these, check out:
-- https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
-- https://developer.roblox.com/en-us/api-reference/datatype/RaycastParams
local result = workspace:Raycast(origin, direction, params)

if result then
    local distance = (origin - result.Position).Magnitude
    local p = Instance.new("Part")
    p.Anchored = true
    p.CanCollide = false
    p.Size = Vector3.new(0.1, 0.1, distance)
    p.CFrame = CFrame.new(origin, position)*CFrame.new(0, 0, -distance/2)
end
2 Likes

what does the position thingy in the second to last line stand for?

It should be result.Position

					if Visualize then
						local dist = (rayOrigin - raycastResult.Position).Magnitude
						local part = Instance.new("Part", workspace)
						part.Anchored = true
						part.CanCollide = false
						part.Color = Color3.fromRGB(255,0,0)
						part.Size = Vector3.new(0.1, 0.1, dist)
						part.CFrame = CFrame.new(rayOrigin, raycastResult.Position)*CFrame.new(0, 0, -(dist/2))
					end

Try this, I tried to use your variables so you can adapt it better to your code.

function CreateVisualRayPart()
	local part = Instance.new("Part", workspace)
	part.Anchored = true
	part.CanCollide = false
	part.Color = Color3.fromRGB(255,0,0)
	
	return part
end

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local tip = script.Parent.Tip.Position
local direction = (mousePos - tip).Unit
local magnitude = 300
local visualize = true

local raycastResult = workspace:Raycast(tip, direction * magnitude, raycastParams)

if raycastResult then -- if the ray hit something
	if visualize then
		local rayPart = CreateVisualRayPart()
		rayPart.Size = Vector3.new(0, 0, (tip - raycastResult.Position).Magnitude)
		rayPart.CFrame = CFrame.new((tip + raycastResult.Position)/2, raycastResult.Position)
			-- this uses CFrame.new(startPosition, lookAt) format
				-- finding the middle point between two vectors is like using numbers
				-- to find the middle between 5 and 10, you add 5 + 10 and divide by how many numbers (2)
	end
	-- Rest of code
else -- if the ray didn't hit something
	if visualize then
		local rayPart = CreateVisualRayPart()
		local endPos = tip + (direction * magnitude)
		rayPart.Size = Vector3.new(0, 0, magnitude)
		rayPart.CFrame = CFrame.new((tip + endPos)/2, endPos)
	end
	-- Rest of code
end
3 Likes