Raycast automatically stopped if gun is pointed in air?

Wondering how I can fix this sort of thing.

The raycast works just fine when pointed at the ground, part, etc.

But, when pointed at the air, it automatically stops for some reason?

Here’s an example: https://streamable.com/8raozc

And here’s the raycast code -


	local RaycastParameters = RaycastParams.new()
	local IgnoreItems = {
		Player.Character,
		unpack(Gun:GetChildren()),
		unpack(workspace.RaycastDebris:GetChildren())
	}
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	RaycastParameters.FilterDescendantsInstances = IgnoreItems
	RaycastParameters.IgnoreWater = true
	
	local StudTravel = BulletRange.Value
	local Origin = Handle.ShootPoint.Position
	
	-- Defined below
	local Direction
	
	-- Spread for shotguns, etc.
	if Settings.Spread.Value then
		local NewSpread = GetSpread()
		Direction = (Mouse.Hit.Position - Origin).Unit + NewSpread
		print("Spread (".. tostring(NewSpread).. ")")
	else
		Direction = (Mouse.Hit.Position - Origin).Unit
	end
	
	-- The client raycast (not used for damage)
	local Result = workspace:Raycast(Origin, (Direction * StudTravel), RaycastParameters)
	
	local Cross = Result and Result.Position or Origin + Direction
	local Distance = (Origin - Cross).Magnitude

1 Like

I don’t think it’s stopping, I think what’s happening is that the raycast is literally going to the very very limit of the world. You need to add a max distance for your ray to stop at so that it literally does not go to the infinity realm.

EDIT: okay so it’s not going to the infinity realm, but it is defaulting onto some co-ordinate. The reason I believe this to be the case is because watching your recording, when you fire into the air, this happens on a frame between 0:15 and 0:16 -

okay so edit 2: my bad. I looked again and it actually does just stop because I think there is nothing for it to detect from where it’s hitting.

So it’s either going to the infinity realm (basically the very bounds of the studio too fast) or it’s just not able to detect anything to point the raycast to so it just stops right after it starts.

Off topic but, how did you do the spread? Can you show me? I’ve been trying to do raycast spread for shotguns, I’ve searched but none of them were really helpful so.

1 Like

Yeah, I believe you’re right.

I changed the range of the gun from 500 to 150 and even with parts around the terrain so I couldn’t see the sky. It stopped when pointed at something too far away.

1 Like

yeah, sorry about the wrong edit at first though. I’m still somewhat new to programming and I’ve only just picked up raycasting so I’ll try to figure out the issue correctly before posting in future.

1 Like

Check my threads and there is a solution to your problem. Should be in the thread called “raycast spread” or something.