Hello,I’m working on a gun system and I have encountered a problem within my code somehow It doesn’t go to exact cursor position,the bullet effect.
local Raycast_Parameters = RaycastParams.new()
Raycast_Parameters.FilterDescendantsInstances = {Player.Character}
Raycast_Parameters.FilterType = Enum.RaycastFilterType.Exclude
local Distance = (Camera.CFrame.Position - Camera.Focus.Position).Magnitude
local Mouse_2D_Position = User_Input_Service:GetMouseLocation()
local Mouse_3D_Ray = Camera:ViewportPointToRay(Mouse_2D_Position.X,Mouse_2D_Position.Y)
local Mouse_Position_Result = workspace:Raycast(Mouse_3D_Ray.Origin,Mouse_3D_Ray.Direction * 1000,Raycast_Parameters)
local Fire_Point = Current_Gun_Object.__model:FindFirstChild("FirePoint",true)
local Start_Position = Fire_Point.WorldPosition
local Current_Position = Start_Position
local Previous_Position = Current_Position
local Reach_Position = Mouse_3D_Ray.Origin + Mouse_3D_Ray.Direction * 1000
if Mouse_Position_Result then
Reach_Position = Mouse_Position_Result.Position
end
local Direction_To_Target : Vector3 = (Reach_Position - Start_Position).Unit
local Distance_In = (Reach_Position - Start_Position).Magnitude
local Velocity = Direction_To_Target * 1000
local Bullet_Render = nil
local Bullet = nil
if Required_SH.Disable_Bullets == false then
Bullet = Instance.new("Part",workspace)
Bullet.Name = "Bullet"
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.CanTouch = false
Bullet.CanQuery = false
for Key,Value in Current_Gun_Object.__inherit.Bullet.Properties do
Bullet[Key] = Value
end
local Attachment_Start = Instance.new("Attachment",Bullet)
Attachment_Start.Name = "Start"
Attachment_Start.CFrame = Current_Gun_Object.__inherit.Bullet.Attachments.Start.CFrame
local Attachment_End = Instance.new("Attachment",Bullet)
Attachment_End.Name = "End"
Attachment_End.CFrame = Current_Gun_Object.__inherit.Bullet.Attachments.End.CFrame
local Bullet_Effect = Instance.new("Trail",Bullet)
Bullet_Effect.Name = "Bullet Effect"
Bullet_Effect.Attachment0 = Attachment_Start
Bullet_Effect.Attachment1 = Attachment_End
for Key,Value in Current_Gun_Object.__inherit.Bullet.Effect do
Bullet_Effect[Key] = Value
end
Bullet.CFrame = CFrame.new(Start_Position)
Bullet_Render = function ()
local Delta_Time = Run_Service.RenderStepped:Wait()
Velocity += Vector3.new(0,-workspace.Gravity * Delta_Time,0)
local Next_Position = Current_Position + Velocity * Delta_Time
Current_Position = Next_Position
Bullet.CFrame = CFrame.new(Next_Position)
return Bullet_Render()
end
task.spawn(Bullet_Render)
elseif Required_SH.Disable_Bullets == true then
end
Any help is appreciated,I need essential help.
I am not entirely sure what is going on but, assuming all the variables are accurate, it most likely has something to do with the bullet render function, as it is not actually going towards a specific point, and is just setting a parts position, which can accumulate errors.
How I have done this is by letting the game physics do the work and just using :ApplyImpulse() and letting the bullet travel physically to the point.
If you want to do this in the function, then I would recommend lerping or tweening to the end point, both ways you will actually reach the end point for sure, instead of just advancing in a direction and hoping you reach a specific point.
This is same thing as lerping or applying the impulse,isn’t it,It’s just through recursive function.
I believe User_Input_Service:GetMouseLocation() returns a screen position, which might not be working properly with Camera:ViewportPointToRay()
I don’t know they all have same effect
You are not wrong, I just thought it might be more innacurate, especially over long distances, but floating point errors should actually be minor. I have just had problems with this approach in the past.
@Carbonated_Cat’s response reminds me of something, if the bullet consistantly lands below the mouse point, the screen GUI might have the property “IgnoreGuiInset” off, turn that on, and the mouse position and where the mouse clicked in world should be the same. You can also test this by making a part at Reach_Position and seeing if it lines up with the mouse position.
If this is not the case, then I think you need to share more information on what it looks like.
Alright so what shall I do to debug then?
Oh also the situation is like that
I forgot to send picture of it
The problem isn’t the render loop or floating point error. You calculate the correct Reach_Position, but then you never actually move the bullet toward that point. You just keep advancing it by velocity each frame, so it drifts and never truly lines up with the cursor hit position.
You already know the exact target position from the raycast, so instead of simulating movement like this, you should move the bullet based on progress from start → end (lerp) or point it directly at the target each step.
Right now, it’s “move forward forever in this direction”, not “move to this exact point”, which is why it ends up off.
Hope this helps 
How should I do it without lerp,could you explain it further elaborated?So I might have a chance to do it with velocity too.
Well if you want to debug it yourself, I would verify every position by making an anchored part at the mouse hit position, and just make sure the positions are correct.
Did you turn on the IgnoreGuiInset on the ScreenGUI? It looks like the bullet is hitting the correct spot on the head, but you are very close to the target which might make the difference hard to notice.
Is this only happening on the head?
It is hard to see from my perspective if anything is wrong per se, I would recommend verifying every position and vector. If everything is still correct and the problem persists, you can at least be satisfied that it is not the variables causing this. As a last resort, you can try to recreate this problem in a new game to try and find the problem/recreate it.
(I also don’t actually think it is possible to lerp bullet drop)
You can still use velocity, but you need to recalculate the direction toward the target every frame, not just once at the start.
Right now, you do:
local Direction_To_Target = (Reach_Position - Start_Position).Unit
local Velocity = Direction_To_Target * 1000
and then you keep moving with that forever. If anything changes (gravity, tiny drift, frame time), you’ll slowly miss the exact point.
Instead, each step do:
local direction = (Reach_Position - Current_Position).Unit
Velocity = direction * 1000
Now the bullet is always correcting itself toward the exact hit position while still using velocity, so it won’t drift off the cursor point.
I hope this helps 
How can I set IgnoreGuiInset?I don’t know how to do it
Welp that didn’t work,sadly.I don’t know why
local Raycast_Parameters = RaycastParams.new()
local Blacklist_Table = {Player.Character}
--for _,Children in workspace:GetChildren() do if Children:IsA("Model") and Children:FindFirstChild("Humanoid") then table.insert(Blacklist_Table,Children) end end
Raycast_Parameters.FilterDescendantsInstances = Blacklist_Table
Raycast_Parameters.FilterType = Enum.RaycastFilterType.Exclude
local Distance = (Camera.CFrame.Position - Camera.Focus.Position).Magnitude
local Mouse_2D_Position = User_Input_Service:GetMouseLocation()
local Mouse_3D_Ray = Camera:ViewportPointToRay(Mouse_2D_Position.X,Mouse_2D_Position.Y)
local Mouse_Position_Result = workspace:Raycast(Mouse_3D_Ray.Origin,Mouse_3D_Ray.Direction * 1000,Raycast_Parameters)
local Fire_Point = Current_Gun_Object.__model:FindFirstChild("FirePoint",true)
local Start_Position = Fire_Point.WorldPosition
local Current_Position = Start_Position
local Previous_Position = Current_Position
local Reach_Position = Mouse_3D_Ray.Origin + Mouse_3D_Ray.Direction * 1000
if Mouse_Position_Result then
Reach_Position = Mouse_Position_Result.Position
end
local Direction_To_Target : Vector3 = (Reach_Position - Current_Position).Unit
local Bullet_Render = nil
local Bullet = nil
if Required_SH.Disable_Bullets == false then
Bullet = Instance.new("Part",workspace)
Bullet.Name = "Bullet"
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.CanTouch = false
Bullet.CanQuery = false
for Key,Value in Current_Gun_Object.__inherit.Bullet.Properties do
Bullet[Key] = Value
end
local Attachment_Start = Instance.new("Attachment",Bullet)
Attachment_Start.Name = "Start"
Attachment_Start.CFrame = Current_Gun_Object.__inherit.Bullet.Attachments.Start.CFrame
local Attachment_End = Instance.new("Attachment",Bullet)
Attachment_End.Name = "End"
Attachment_End.CFrame = Current_Gun_Object.__inherit.Bullet.Attachments.End.CFrame
local Bullet_Effect = Instance.new("Trail",Bullet)
Bullet_Effect.Name = "Bullet Effect"
Bullet_Effect.Attachment0 = Attachment_Start
Bullet_Effect.Attachment1 = Attachment_End
for Key,Value in Current_Gun_Object.__inherit.Bullet.Effect do
Bullet_Effect[Key] = Value
end
Bullet.CFrame = CFrame.new(Start_Position)
Bullet_Render = function ()
local Delta_Time = Run_Service.RenderStepped:Wait()
local Velocity = (Reach_Position - Current_Position).Unit * 1000
Velocity += Vector3.new(0,-workspace.Gravity * Delta_Time,0)
local Next_Position = Current_Position + Velocity * Delta_Time
Current_Position = Next_Position
Bullet.CFrame = CFrame.new(Next_Position)
return Bullet_Render()
end
task.spawn(Bullet_Render)
elseif Required_SH.Disable_Bullets == true then
end
I tried workspace as filterdescendantInstances but If I do so they won’t go through head.
Again,Any help is still appreciated!
I found my way out from this.Thanks to ai for helping me to understand the logic behind it,I then fixed it myself afterwards.