Raycast not getting any results

This did not fix my problem. It is still returning no results.

In the original script, try to change this

to this

local RayDirection = Vector3.new(math.random(-30, 30), math.random(-30,30), math.random(-30, 30))
local result = workspace:Raycast(startpos, RayDirection)

That’s exactly what I tried

()()()(

More tips:

If it does not return a result it’s a good idea to debug by visualizing the ray. Using a part in the below method:

It will give you more information on where the rays are going instead of just saying oh it just doesn’t work.

Second point is that, the script uses a random direction, and only runs 15 times. Theres always a possibility it doesn’t hit anything, or the ray clips inside another part and doesn’t hit the surface as such.

Always assume that every line of your code has a hidden logic error.

With rays always draw the ray:

function DrawRay(origin, point) – 7 studs long – not a point; a dir
local Ray = Ray.new(origin, (point).unit * 7) --Make the ray.
local Hit,Position = game.Workspace:FindPartOnRay(Ray,AI) --Check for collisions along the ray, ignoring any Parts of us.

if true then --Graphics
local RayPart = Instance.new(“Part”,AI)
if Hit then
if Logic == 1 then
RayPart.BrickColor = BrickColor.new(“Black”) --Set its color.
else
RayPart.BrickColor = BrickColor.new(“Bright red”) --Set its color.
end
else
if Logic == 1 then
RayPart.BrickColor = BrickColor.new(“White”) --Set its color.
else
RayPart.BrickColor = BrickColor.new(“Olive”) --Set its color.
end
end
RayPart.Transparency = 0.2 --Set its transparency.
RayPart.Anchored = true --Set whether it will fall or not.
RayPart.CanCollide = false --Set whether people can walk though it or not.
RayPart.formFactor = Enum.FormFactor.Custom --Make it so it can be small.
local Distance = (Position-origin).magnitude --Find the distance between the hit and the torso.
RayPart.Size = Vector3.new(0.4,.2,Distance) --Set its size to the distance.
RayPart.CFrame = CFrame.new(Position, origin) * CFrame.new(0,0,-Distance/2) --Move it halfway.
game.Debris:AddItem(RayPart,2) --Add it to the debris.
end – Graphics

return Hit
end – DrawRay

AI - Roblox

I think it is best if i provide a place with the non-working version of the script and a sort of working one to give you a better idea. I am visualizing it.

Ray.rbxl (33.8 KB)

What is “AI”?

[][][][][][][]][][]

AI - Roblox

It’s the model shooting the ray and a place to put the drwings, so that you don’t count those as hits.

This is just an example for you to canabalize

direction = startPos + Vector3.new(math.random(-30, 30), math.random(-30,30), math.random(-30, 30))

where exactly are you trying to cast the ray to?

I’m trying to cast a bunch of rays that all start at the same position but go in random directions. I want the “tracer”, or the part visualizing the ray, to only be as long as the distance between the starting point and the point where it collides with something. Think of a shotgun.

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

1 Like

Modified this slightly

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(-15, 15), math.random(-15,15), math.random(-15, 15))
    local result = workspace:Raycast(startpos, RayDirection, Params)

    if result then
        local hit = result.Instance
        local endpos = hit.Position
        local distance =  (startpos - endpos).Magnitude

        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

        spawn(function()
            for i = 1, 100 do
                tracer.Transparency = tracer.Transparency + 0.01
                wait(0.02)
            end
            tracer:Destroy()
        end)
    end
end

for i = 1, 20 do
    createTracer(part.Position)
    wait(0.02)
end

It’s working, but all the parts are not spreading like I want them to. Instead they’re all appearing at the same position

If you want it to spread randomly spherically kinda, then remove start position at all.

    local RayDirection = Vector3.new(math.random(-15, 15), math.random(-15,15), math.random(-15, 15))
1 Like

Now nothing is appearing. It’s not even printing. We’re going in circles.

The issue with your code is that you are only visualizing the ray only if it hits and gets a result.

Shoulda copied by other code I linked but I’ll post it again.

This is what it looks like when you remove startpos which is what I believe what you wanted but you never specified clearly.

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

The rays should be going towards a general direction and not be omnidirectional. Y’know, like a shotgun spray behaves.

Then what is the purpose of the “if result then” block?

I’m doing it that way because I want the part to be just perfectly sized, not extending past the part it hit, not missing by just a few studs, but just perfect.

Basically, the first part the ray collides with, that’s how far the part should extend.

Oh now you mention shotgun spray? Shouldn’t you have mentioned that in post 1? :upside_down_face:

So you are doing a raycast shotgun spray from a parts position towards the origin at (0,0,0)?

Then the problem is that the random vector offsetting method is problematic the further away the target position is which seems to be origin if you do -StartPosition. This is further explained below:

Here is another method if you want to keep the vector offset method but standardize the distance problem:

I believe I’ve had enough for today hope this helps.

ok

No, I intend to have the spray aimed at where the player wants soon. This is just one part of a whole thing I’m making.

Also, I don’t quite understand. What does this have to do with my problem?

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)

	if result then
		local endpos = result.Position
		local distance =  (startpos - endpos).Magnitude

		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

		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

These produced the results I wanted. Literally all I had to do there I think was change “local hit = result.Instance” to “local endpos = result.Position” The parts visualizing the rays now stop directly at the surface of the baseplate, which was what I wanted. Thank you everyone for helping.

That was one of my first projects: Shrapnel graphics and all. Close enough and you can blow the limbs off players, but won’t kill you, unless it blows off Head.

But Filtering broke it, and it’s just a bunch of fudge.

GL