No changes in code but different results?

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 !!!

I’ve noticed minor differences between the scripts, however I could be completely wrong about the following differences and its effects on the calculations, but just stating some key differences.

  1. Not sure if this could be a cause but what’s the value of travel Distance? As in the other script its set at 100.

  2. Also I’ve noticed how these scripts differ is that in the working script at

You use ViewportPointToRay, whereas in the second one you use ScreenPointToRay.

Could any GUI or something impact the calculations when using ScreenPointToRay? Difference between :ViewportPointToRay() and :ScreenPointToRay()

  1. Also in the issue script I’ve noticed you used Ray.new for the bullet ray, as Ray.new is depreciated maybe this could be causing difference results? As although depreciated functions can still be used, its encouraged to use the newer ones. (probs wont make a difference?)
    Is it still possible to use deprecated items?

Other than that, all the calculations seem the same to me. Hopefully this was helpful.

1 Like
local origin = muzzleAttachment.WorldPosition
	local mouseLocation = UserInputService:GetMouseLocation()
	local viewportRay = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local viewportDirection = viewportRay.Direction * 100
	local viewportRaycast = Workspace:Raycast(viewportRay.Origin, viewportDirection, raycastParameters)
	local viewportIntersection = viewportRaycast and viewportRaycast.Position or viewportRay.Origin + viewportDirection
	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

Thanks, I’ve made the changes you suggested but I just cant seem to figure out whats causing the issue, either im missing something really stupid or it can be something else outside of the script that’s causing this.

Okay, I may have found an older version of the code that works, let me experiment with it

Okay, so the issue is that when the travel distance is lower than the current zoom from the camera, it will try to fire to your camera, gonna make another post about this, thanks!

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