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.
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
And i get the mouse position like this
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
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
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
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
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.
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
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