Yeah I tested it out in studio, @goofylamb83 had the wrong visualization formula, there is supposed to be -direction/2
This is the resultant, it hits the air and is pretty obvious.
local RayDirection = -startpos+Vector3.new(math.random(-30, 30), math.random(-30,30), math.random(-30, 30))
local result = workspace:Raycast(startpos, RayDirection,Params)
local distance = RayDirection.Magnitude
local endpos = startpos + RayDirection
tracer.CFrame = CFrame.new(startpos , endpos) * CFrame.new(0, 0, -distance/2) -- the correct formula
--Previously, the one @comfycouchman was using you can download in the rbxl file
-- tracer.CFrame = CFrame.new(startpos , endpos) * CFrame.new(0, 0, distance/2) -- this is the wrong one
You will need to do -startPos for it to go towards the origin direction
Code
local part = script.Parent.red
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Whitelist
Params.IgnoreWater = true
Params.FilterDescendantsInstances = {workspace}
local PhysicsService = game:GetService(“PhysicsService”)
PhysicsService:CreateCollisionGroup(“NonCollide”)
PhysicsService:CollisionGroupSetCollidable(“NonCollide”,“Default”,false)
local function createTracer(startpos)
local RayDirection = -startpos+Vector3.new(math.random(-30, 30), math.random(-30,30), math.random(-30, 30))
local result = workspace:Raycast(startpos, RayDirection,Params)
local distance = RayDirection.Magnitude
local endpos = startpos + RayDirection
local tracer = Instance.new("Part")
tracer.Color = Color3.fromRGB(163, 162, 165)
PhysicsService:SetPartCollisionGroup(tracer,"NonCollide")
tracer.Material = Enum.Material.Neon
tracer.CanQuery = false -- prevent raycasting hitting this
tracer.Anchored = true
tracer.CanCollide = false
tracer.Size = Vector3.new(0.5, 0.5, distance)
tracer.CFrame = CFrame.new(startpos , endpos) * CFrame.new(0, 0, -distance/2)
tracer.Parent = workspace
if result then
local hit = result.Instance
print(hit)
local endpos = hit.Position
local distance = (startpos - endpos).Magnitude
spawn(function()
for i = 1, 100 do
tracer.Transparency = tracer.Transparency + 0.01
wait(0.01)
end
tracer:Destroy()
end)
end
end
for i = 1, 100 do
createTracer(part.Position)
wait(0.05)
end