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.
-- 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
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