Bullet fires backwards?

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?)
Screenshot 2024-12-23 194504
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
1 Like

how are you visualizing the bullet (code)?

1 Like
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
	fireRemote:FireServer(origin, bulletDirection, bulletRaycast and bulletRaycast.Instance)
-- VISUALIZATION***
	local part = Instance.new("Part")
	part.Size = Vector3.new(0.1, 0.1, distanceBulletTraveled)
	part.CFrame = CFrame.lookAt(bulletIntersection, origin) * CFrame.new(0, 0, -distanceBulletTraveled / 2)
	part.Anchored = true
	part.CollisionGroup = "Bullets"
	part.CanCollide = false
	part.Parent = Workspace

Here’s the full code snippet

I am having trouble replicating the issue as that code works fine for me

local UserInputService = game:GetService("UserInputService")

local muzzleAttachment = script.Parent.Handle.Muzzle
local currentCamera = workspace.CurrentCamera

local raycastParameters = RaycastParams.new(
	
)

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then

		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

		-- VISUALIZATION***
		local part = Instance.new("Part")
		part.Size = Vector3.new(0.1, 0.1, distanceBulletTraveled)
		part.CFrame = CFrame.lookAt(bulletIntersection, origin) * CFrame.new(0, 0, -distanceBulletTraveled / 2)
		part.Anchored = true
		part.CollisionGroup = "Bullets"
		part.CanCollide = false
		part.Parent = Workspace
	end
end)
1 Like

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

If none of these show any issues, I can send you the place file if you want, otherwise it is some other issue with a different thing

yes setting the travel distance to 25 causes this issue, I just increased it and its fixed I think?

Yeah, that is what im trying to fix though, like for example if I want a gun like a shotgun that shoots super close then I cant set it that low.

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

This I believe does what you want with having a maximum bullet length but still aiming towards the right spot

local UserInputService = game:GetService("UserInputService")

local muzzleAttachment = script.Parent.Handle.Muzzle
local currentCamera = workspace.CurrentCamera

local raycastParameters = RaycastParams.new(
	
)

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then

		local mouseTravelDistance = 1000
		local origin = muzzleAttachment.WorldPosition
		local mouseLocation = UserInputService:GetMouseLocation()
		local viewportRay = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
		local viewportDirection = viewportRay.Direction * mouseTravelDistance
		local viewportRaycast = Workspace:Raycast(viewportRay.Origin, viewportDirection, raycastParameters)
		local viewportIntersection = viewportRaycast and viewportRaycast.Position or viewportRay.Origin + viewportDirection
		
		
		local bulletTravelDistance = 25
		local bulletDirection = (viewportIntersection - origin).Unit
		local bulletRaycast = Workspace:Raycast(origin, bulletDirection, raycastParameters)
		local bulletIntersection = bulletRaycast and bulletRaycast.Position or viewportIntersection
		local distanceBulletTraveled = math.clamp((origin - bulletIntersection).Magnitude, 0, bulletTravelDistance)

		-- VISUALIZATION***
		local part = Instance.new("Part")
		part.Size = Vector3.new(0.1, 0.1, distanceBulletTraveled)
		part.CFrame = CFrame.lookAt(origin, bulletIntersection) * CFrame.new(0, 0, -distanceBulletTraveled / 2)
		part.Anchored = true
		part.CollisionGroup = "Bullets"
		part.CanCollide = false
		part.Parent = Workspace
	end
end)

demo: Watch 2024-12-24 17-55-44 | Streamable

1 Like

Yeah, this ended up working thank you so much, I spent so much time trying to figure this issue out

1 Like

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

I Believe the bullets are aiming at what the mouse is gonna hit in 3D?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.