Raycast gun shoot in wrong directions sometimes

  1. What do you want to achieve? Hello, i am currently working on a gun system for my game and the gun sometimes shoots in the wrong direction.

  2. What is the issue? Like i said, the gun shoots in the wrong direction, the issue seems to be the mouse position that sometimes goes up i don’t know why. You can look this video :
    2022-12-18 11-27-43

This is how the bullet starts
image

And i get the mouse position like this
image

  1. What solutions have you tried so far? I tried to fix it myself and looked on forums.

Im pretty sure for raycasting, you should be using workspace:Raycast() instead of workspace:FindPartOnRay()

As for the Direction, why are you getting a CFrame and then converting it to a vector?

If your gun system is first person, you can use the cameras LookVector multiplied by the gun range for the ray direction.

local GunRange = 500
local GunStartPosition = Camera.CFrame.Position

local ShootParams = RaycastParams.new()
--[[ if you want, you can add the character and anything else to the ray blacklist using this: 
RayParams.FilterDescendantsInstances = {Filter goes in here}
]]
local RayDirection = Camera.CFrame.LookVector * GunRange
local Result = workspace:Raycast(GunStartPosition, RayDirection, ShootParams)

if Result then
    print('Ray hit something')
end

Else, if your system is third person, you can use this:

local GunRange = 500
local GunStartPosition = Camera.CFrame.Position

local ShootParams = RaycastParams.new()
local RayDirection = ((Camera.CFrame.Position - Mouse.Hit.Position).Unit * -GunRange)
local Result = workspace:Raycast(GunStartPosition, RayDirection, ShootParams)

if Result then
    print('Ray hit something')
end
1 Like

My gun now output this

image
Direction is causing the error

If youre using the raycasting code i supplied as the MousePosition, the code sometimes returns nil, as the ray didnt hit anything, you could have shot a bullet into the sky or a wall that was out of the rays range.

You’ll also need to return Result.Position when returning from the code i supplied, as Result is a table, containing a Surface Normal (The rotation of the surface hit by the ray) The part hit by the ray, the position of the hit and so on.

Make sure that the GunStartPosition also gets updated whenever you call the function in the code i gave, as it would just be 0, 0, 0 or nil without getting updated.

local GunRange = 500 -- 500 is an example number
function GetMousePosition()
    local GunStartPosition = Camera.CFrame.Position
    local ShootParams = RaycastParams.new()
    local RayDirection = ((Camera.CFrame.Position - Mouse.Hit.Position).Unit * -GunRange)
    local Result = workspace:Raycast(GunStartPosition, RayDirection, ShootParams)
    
    if Result and Result.Position then
        return Result.Position
    else
        return Camera.CFrame.LookVector * GunRange --[[
        Just have this as a backup in case the ray doesnt hit anything, just to ensure a bullet gets shot
        ]]
    end
end
1 Like

My GetMousePosition now looks like this

But the gun is still shooting in wrong directions

And this getmouseposition is not mobile compatible, because my raycast needs to always shoot at the center of the screen, and on mobile if i press the shoot button it dosen’t shoot at the center of the screen it just shoots the shoot button on the right

(My gun system is first person btw)

By the looks of the video (which i should have watched before my first post) it seems that your rays are maybe colliding with your bullet models.

Im not sure how workspace:FindPartOnRay works, but using workspace:Raycast you can have a blacklist.
I would recommend having a folder for the bullets, and getting children of that folder as the blacklist.

local BlacklistTable = workspace.BulletsFolder:GetChildren() or {}
table.insert(BlacklistTable, Character)
RayParams.FilterDescendantsInstances = BlacklistTable

As for the gun system not working on mobile, you can switch out a single line of code and its fixed.

local GunRange = 500 -- 500 is an example number
function GetMousePosition()
    local GunStartPosition = Camera.CFrame.Position
    local ShootParams = RaycastParams.new()

    local BlacklistTable = workspace.BulletsFolder:GetChildren() or {}
    table.insert(BlacklistTable, Character)
    ShootParams.FilterDescendantsInstances = BlacklistTable

    local RayDirection = Camera.CFrame.LookVector * GunRange -- Changed this line
    local Result = workspace:Raycast(GunStartPosition, RayDirection, ShootParams)
    
    if Result and Result.Position then
        return Result.Position
    else
        return Camera.CFrame.LookVector * GunRange
    end
end
1 Like

My getmouseposition now looks like this
image

But the gun still have shooting issues

I would argue that its better to use Character.PrimaryPart.Position for the starting point of the ray, since the params are filtering the Descendants of the character, it would be ideal as there is nothing obscuring it

Plus, The Cameras Position and Orientation is constantly changing.

1 Like

It seems to have fixed the issue but the gun is shooting at my foot

Starting Position, not its Direction :confused:

Like that ?
image

(I meant it’s shooting from my foot, sorry)

General way of doing it:

local RCP = RaycastParams.new()
RCP.FilterDescendentsInstances = {player.Character}


local RCR = workspace:Raycast(GunStartPosition, (GunStartPosition - mousepos)* Range, RCP)

if RCR then
  return RCR.Position
else
  return mousepos
end

With what do i replace this with

Range = 500 -- stole this from EZQP3 lol
function GetMousePosition()
    local GunStart = player.Character.PrimaryPart

    local RCP = RaycastParams.new()
RCP.FilterDescendentsInstances = {player.Character}


local RCR = workspace:Raycast(GunStart.Position, (GunStart.Position - mousepos)* Range, RCP)

if RCR then
  return RCR.Position
else
  return mousepos
end
end

if you want to get the Intersected Point within a ray, Its RCR.Position + RCR.Normal

1 Like

How can i get the mousepos from the getmousepos function, i think i explained wrong my scripts

The GetMousePosition function is in a localscript and this :
image
This is in a script

Usually I would Use a RemoteEvent for this, from using FireServer, you can insert Mouse.Hit.p and it should work