I just need help figuring out how these scripts are different, because one gives me an issue and the other doesn’t. (My issue is that one will fire towards the camera when I zoom out, but the other doesn’t, IF you need more information please tell me, but right now I just want to know what the difference is)
WORKING:
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
visualizeLocalShotEvent:Fire(muzzle, bulletIntersection, bulletLength)
fireRemote:FireServer(bulletRay.Origin, bulletRay.Direction, bulletResultInstance)
local part = Instance.new("Part")
part.Size = Vector3.new(0.1, 0.1, bulletLength)
part.CFrame = CFrame.lookAt(muzzlePosition, bulletIntersection) * CFrame.new(0, 0, -part.Size.Z / 2)
part.Anchored = true
part.CollisionGroup = "Bullets"
part.CanCollide = false
part.Parent = Workspace
ISSUE CODE:
local origin = muzzleAttachment.WorldPosition
local mouseLocation = UserInputService:GetMouseLocation()
local viewportRay = currentCamera:ScreenPointToRay(mouseLocation.X, mouseLocation.Y)
local viewportRayDirection = viewportRay.Direction * 100
local viewportRaycast = Workspace:Raycast(viewportRay.Origin, viewportRayDirection, raycastParameters)
local viewportIntersection = viewportRaycast and viewportRaycast.Position or viewportRay.Origin + viewportRayDirection
local bulletRay = Ray.new(origin, (viewportIntersection - origin).Unit * 100)
local bulletRaycast = Workspace:Raycast(bulletRay.Origin, bulletRay.Direction, raycastParameters)
local bulletIntersection = bulletRaycast and bulletRaycast.Position or viewportIntersection
local distanceBulletTraveled = (origin - bulletIntersection).Magnitude
fireRemote:FireServer(origin, bulletRay.Direction, 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
If anyone has a good eye, please help! I have gone over the issue at least 3 times now and I haven’t found any differences relating to the equations/calculations !!!