Raycast distance and shooting into the sky

I have a gun script with a set max distance, but for some reason the gun raycasts/shoots past the max distance. And also, when I shoot at the sky it doesn’t return a raycastResult. Can anyone help??

local tool = script.Parent
local fireEvent = tool.FireEvent
local firePoint = tool.Muzzle.Attachment
local debris = game:GetService("Debris")

local config = script.Parent.Configuration
local maxdist = config.MaxDistance.Value
local spread = config.Spread.Value

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true

local function onEquip()
   raycastParams.FilterDescendantsInstances = {tool, tool.Parent}
end

local function fire(mousePoint)
   local fireDistance = (tool.Muzzle.Attachment.WorldPosition - mousePoint).Magnitude
   local finalPoint = Vector3.new((mousePoint.x)+(math.random(-(spread/10)*fireDistance,(spread/10)*fireDistance)),(mousePoint.y)+(math.random(-(spread/10)*fireDistance,(spread/10)*fireDistance)),(mousePoint.z)+(math.random(-(spread/10)*fireDistance,(spread/10)*fireDistance)))
   
   local rayDirection = (finalPoint - tool.Muzzle.Attachment.WorldPosition).Unit * maxdist
   local raycastResult = workspace:Raycast(tool.Muzzle.Attachment.WorldPosition, rayDirection)
   
   if raycastResult then
   	local part = Instance.new("Part")
   	part.Parent = workspace
   	part.Anchored = true
   	part.Position = raycastResult.Position
   	print("Instance:", raycastResult.Instance)
   	print("Position:", raycastResult.Position)
   	print("Distance:", raycastResult.Distance)
   	print("Material:", raycastResult.Material)
   	print("Normal:", raycastResult.Normal)
   end
end

fireEvent.OnServerEvent:Connect(function(player, mousePoint)
   fire(mousePoint)
end)

tool.Equipped:Connect(onEquip)
1 Like

for when it doesnt return raycastResult just replace the result distance with maxDistance.
raycast result is returning only in case if it hit something

2 Likes

“replace the result distance with maxDistance”
How would I do that exactly?

if raycastResult then
   distance = raycastResult.Distance
else
   distance = maxDistance
end

You can simplify this with a ternary to save a few lines, but your mileage may vary in terms of readability:

distance = raycastResult and raycastResult.Distance or maxDistance

No no, I mean how would I actually make the beam stretch from the origin to the vector3 that is the max distance.

replace origin

part.CFrame = origin * CFrame.Angles(math.rad(math.random(-(SpreadCone/2), SpreadCone/2)), math.rad(math.random(-(SpreadCone/2), SpreadCone/2)), 0) * CFrame.new(0, 0, -distance/2)

spread will be random, but yeah.

or if you dont want spread then

part.CFrame = origin*CFrame.new(0, 0, -distance/2)

if this is not what you want, then say

Is there a way to integrate the “finalPoint” variable into that since the spread is in the finalPoint?

And origin is the WorldCFrame of the muzzle attachment right?

  1. it can be, but i would not waste time on that, not important
  2. ye. but i using just CFrame instead of WorldCFrame

And also, I’m trying to make the part stretch from the muzzle to the finalPoint (or the max distance that it CAN go if shot at the sky)

So the final line should be like:

part.CFrame = CFrame.new(tool.Muzzle.Attachment.WorldCFrame, the position here)

wdym by scretch? like momentally, or it will move

This is a gun that clanning games use, so it doesn’t shoot bullets, but rather beams that disappear after like 0.1 seconds


then its what you want

try this

if raycastResult then
     distance = raycastResult.Distance
     part.CFrame = CFrame.new(tool.Muzzle.Attachment.WorldCFrame, raycastResult.Position)*CFrame.new(0, 0, -distance/2)
else
   distance = maxDistance
   part.CFrame = (tool.Muzzle.Attachment.WorldCFrame * CFrame.Angles(math.rad(math.random(-(SpreadCone/2), SpreadCone/2)), math.rad(math.random(-(SpreadCone/2), SpreadCone/2)), 0)) * CFrame.new(0, 0, -distance/2)
end

It shoots WAY off. Like I can’t even see where it shot until I move my camera.


always?
try to play with placement of elements

Hm… nevermind that. Instead, how can I find the max Vector3 of the point where the player shot if they shot into the sky or past the maxdistance?

tool.Muzzle.Attachment.WorldCFrame + Vector3.new(0, 0, maxDistance)

oh, i think, you can actually do
local spread = math.random(-(SpreadCone/2), SpreadCone/2))
on the shoot begin,
and then use it on raycast and beam