Why won't my ray detect anything?

Hello everyone,

So my problem is that i’m trying to have a ray come out of a gun to detect what it hits but so far it just says it’s not colliding with anything which is weird because I set the position and direction to be in the direction the gun is pointing. Also the bullet is supposed to come out of the end of the gun and in the direction of the gun so I could use the bullet too for this, but using the bullet won’t work either. It just says nothing collides. Here is my code:

``local raycastResult = workspace:Raycast(gun.Position, gun.CFrame.LookVector.Unit)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.Baseplate}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		
	if raycastResult then
			print("Instance:", raycastResult.Instance)
			print("Position:", raycastResult.Position)
			print("Distance:", raycastResult.Distance)
			print("Material:", raycastResult.Material)
			print("Normal:", raycastResult.Normal)
	else
			warn("No raycast result!")
	end``

So does anyone know what’s going on here and why the ray won’t detect what’s in front of the gun like it clearly should? Thank you.

Sometimes I make mistakes where the gun part isn’t facing the right way and it shoots the ray backwards.

Have you checked if it is backwards or not? You can visualize the ray with Parts to see what went wrong.

... --assuming you have Origin, UnitVector and RaycastResult variables.
local Endpoint = Origin+UnitVector
if RaycastResult then
    Endpoint = RaycastResult.Position
end
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.CanQuery = false
part.CanTouch = false
part.Transparency = 0.5
part.Color = Color3.new(1,0,0)
part.CFrame = CFrame.new((Origin+Endpoint)/2,Endpoint)
part.Size = Vector3.new((Origin-Endpoint).magnitude,1,1)
part.Parent = workspace
game:GetService("Debris"):AddItem(part,1)

Hope that helps!

1 Like

How could I visualize it? That would help a lot. Thanks.

I didn’t see your reply while editing, I edited the reply to include the method to visualize it.

Thank you for your answer. I know how to get Origin like RaycastOrigin.Origin right, but how do I get its UnitVector again? Again thanks for helping me out.

You already have the unitvector!
gun.CFrame.LookVector.Unit is the unitVector.

As it seems, its currently at 1. Your gun could only detect 1 stud away from the barrel. You should add a multiplier infront of it to enlarge the distance.

i.e.
gun.CFrame.LookVector.Unit * 500 (to have 500 studs of range)

1 Like

Sorry, but it says that Origin is not a valid member of RaycastResult. Did I do something wrong? Do you know how I can get the Origin for this as well? Thanks a lot.

the Origin is gun.Position, the very first argument fed to workspace:Raycast.

1 Like

Okay, now I can see the ray! Thank goodness! Thanks a lot, you wouldn’t believe how off this ray was! Almost different continents. Thanks so much, if I have any more questions i’ll ask here and i’ll try to see what’s going on in the code.

Hello, i’ve managed to get the direction right but the brick won’t grow longer, it’s just a little square stud that points in the right direction. Do you know how to fix this? Thanks.

are you changing the unitVector on both the part and the ray?

This is what I have:

local RaycastResult = workspace:Raycast(gun.Position, (gun.CFrame.LookVector.Unit * 50))

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.Baseplate}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		
	if RaycastResult then
			print("Instance:", RaycastResult.Instance)
			print("Position:", RaycastResult.Position)
			print("Distance:", RaycastResult.Distance)
			print("Material:", RaycastResult.Material)
			print("Normal:", RaycastResult.Normal)
	else
			warn("No raycast result!")
	end
		
		local Origin = gun.Position
		
		local Endpoint = Origin + gun.CFrame.LookVector.Unit * 50
		if RaycastResult then
			Endpoint = RaycastResult.Position
		end
		local part = Instance.new("Part")
		part.Anchored = true
		part.CanCollide = false
		part.CanQuery = false
		part.CanTouch = false
		part.Transparency = 0.5
		part.Color = Color3.new(1,0,0)
		part.CFrame = CFrame.new((Origin+Endpoint)/2,Endpoint)
		part.Size = Vector3.new((Origin-Endpoint).magnitude,1,1)
		part.Parent = workspace
		game:GetService("Debris"):AddItem(part,1)

And then it produces a stud that has right orientation that attaches to the end of the arm with the gun.