When I zoom out my camera far away and fire a bullet, it shoots backwards? I dont know how to fix this, please help
The location of my mouse when firing: (It should go forward?)
The result: (it fires backwards)
local travelDistance = 100
local origin = muzzleAttachment.WorldPosition
local mouseLocation = UserInputService:GetMouseLocation()
local viewportRay = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local viewportDirection = viewportRay.Direction * travelDistance
local viewportRaycast = Workspace:Raycast(viewportRay.Origin, viewportDirection, raycastParameters)
local viewportIntersection = viewportRaycast and viewportRaycast.Position or viewportRay.Origin + viewportDirection
local bulletDirection = (viewportIntersection - origin).Unit * travelDistance
local bulletRaycast = Workspace:Raycast(origin, bulletDirection, raycastParameters)
local bulletIntersection = bulletRaycast and bulletRaycast.Position or viewportIntersection
local distanceBulletTraveled = (origin - bulletIntersection).Magnitude
Try zooming out super far, also if that doesnt change anything, set the travel distance lower to like 25 for example and zoom out and also try shooting
I kind of have an idea of what the issue could be, but I cant really explain it: I believe the reason for this is when the length of the ray where it SHOULD cast is longer than the distance from my camera to the origin point (i.e. the muzzle of the gun), and it somehow ends up casting the ray to my camera.
We definitely know now that it is something to do with the direction of the ray and the length because the issue is caused by travelDistance, so that helps
Actually, I found some issue with this, since it uses 1000 as the travel distance, it will actually think that my mouse is aimed at something 1000 studs away if that explains it
As you see, the bullet length is 25, but the code thinks that I am aiming my mouse 1000 “studs” away
So basically, the shot isn’t aimed directly at my mouse, which means that the aiming cold be inaccurate. I am still gonna keep your post as the solution for now until I figure something out, thanks for helping though