Zooming out and firing gun makes bullet shoot to my camera?

If I zoom out far enough, if I fire a bullet, it will go straight to my camera, I’m not sure what causes this?

local function fire()
	local origin = muzzleAttachment.WorldPosition
	local mouseLocation = UserInputService:GetMouseLocation()
	local viewportRay = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local viewportRaycast = Workspace:Raycast(viewportRay.Origin, viewportRay.Direction * 100, raycastParameters)
	local viewportIntersection = viewportRaycast and viewportRaycast.Position or viewportRay.Origin + viewportRay.Direction
	local bulletDirection = (viewportIntersection - origin).Unit * 100
	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)
	local part = Instance.new("Part")
	part.Size = Vector3.new(0.1, 0.1, distanceBulletTraveled)
	part.CFrame = CFrame.lookAt(origin, bulletIntersection) * CFrame.new(0, 0, -part.Size.Z / 2)
	part.Anchored = true
	part.CollisionGroup = "Bullets"
	part.CanCollide = false
	part.Parent = Workspace
end

the issue is likely due to how the bullet direction is calculated in relation to the camera when zoomed out. since you’re using viewportpointtoray to calculate the bullet’s direction, it’s possible that the bullet is aiming toward the cameras position rather than a proper point in the world. this can happen when the mouse is far from the center of the screen especially when zooming out.

to fix this, you can adjust the direction of the bullet by ensuring it’s always fired in the direction from the muzzle not directly toward the camera. you could try using a more consistent method of raycasting or make sure the mouse input is correctly adjusted.

5 Likes

im not that good at coding but i can tell my friend to make one that can solve the problem

5 Likes

if it helps, this is the old code I used, This one worked normally

local bulletSpreadRandomizer = Random.new(Workspace:GetServerTimeNow() * 100000) 
		local travelDistance = tool:GetAttribute("TravelDistance")
		local mouseLocation = UserInputService:GetMouseLocation()
		local viewportPointToRay = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
		local muzzlePosition = muzzle.WorldPosition
		local viewportRayDirection = viewportPointToRay.Direction * travelDistance
		local viewportRaycast = Workspace:Raycast(viewportPointToRay.Origin, viewportRayDirection, raycastParameters)
		local viewportIntersection = viewportRaycast and viewportRaycast.Position or viewportPointToRay.Origin + viewportRayDirection
		local bulletRay = Ray.new(muzzlePosition, (viewportIntersection - muzzlePosition).Unit * travelDistance)
		local bulletRaycast = Workspace:Raycast(bulletRay.Origin, bulletRay.Direction, raycastParameters)
		local bulletIntersection = bulletRaycast and bulletRaycast.Position or viewportIntersection
		local bulletLength = (muzzlePosition - bulletIntersection).Magnitude
		local bulletResultInstance = bulletRaycast and bulletRaycast.Instance

hey can i ask you sm do you use bullets in the workspace in just want to damege when fire without bullet what names you gave them in workspace if yes

4 Likes

bro I cant understand what you are saying and you use ai responses

i mean what is the name of the bullet you gave it in work space

4 Likes

becouse i need to make a funciuon that activates and makes a new bullet that gets fired in the world posision

4 Likes

I’m pretty sure this response is actually AI generated…

yeah I figured that based on the way he talks in his other comments

i mean i just want to know the name in workspace you have given to the bullet in workspace so it gets created

4 Likes

bro you are just solution farming lol, the bullet is named “Part” because I didnt assign a name to it

AI responses starts usually with “The issue is likely due to…”
So yeah, This person is using AI.

that is all that i wanted becouse i had made it with a different name.

4 Likes

What are you making “with a different name”?

I assume they gave it a Name instead of a “Part”

so it seems found the problem you are facing the problem is that Roblox has a limit and if you zoom too far out it cant find your character or you didn’t create a viewportraycase.So both are fixable if you didn’t create a viewportraycase just create it and if you go so far that Roblox cant find your character that you need to add a fallback.I seemed like an artificial inteligence because i saw other people speak respectfully with a good vocabulary and i wanted to be straight and helpful.i haven.t finished the code it will be ready in a bit.

4 Likes

a good fix could be to add to the bottom
if mouseLocation = nil then
Part.Position = BulletRaycast
end
so it dosent glitch out but iam going to test it myself to be shure

4 Likes