Innacurate mouse hit position when clicking on the void

Behaviour:

Script to find MouseHitPosition:

function Module.MouseHitPosition(Mouse, Camera, Character)

local HitPart
local HitPosition

local Ignore = {Character, workspace.Objects}
local UnitRay = Camera:ViewportPointToRay(UserInputService:GetMouseLocation().X, UserInputService:GetMouseLocation().Y)

local RayParams = RaycastParams.new()
RayParams.IgnoreWater = true
RayParams.FilterType = Enum.RaycastFilterType.Blacklist

while true do
	RayParams.FilterDescendantsInstances = Ignore
	local RaycastResult = workspace:Raycast(UnitRay.Origin, UnitRay.Direction * 1e12, RayParams)
	if RaycastResult ~= nil then
		HitPart = RaycastResult.Instance
		HitPosition = RaycastResult.Position
		if Module.IgnoreObjects(HitPart) == true then
			table.insert(Ignore, HitPart)
			HitPart = nil
			HitPosition = UnitRay.Origin + UnitRay.Direction * 1e12
		else
			break
		end
	else
		HitPart = nil
		HitPosition = UnitRay.Origin + UnitRay.Direction * 1e12
		break
	end
end

return HitPosition, HitPart, UnitRay.Origin + UnitRay.Direction * 1e12

end

This looks like it’s just a perspective thing. The hit position in the video is 1012 studs away, so while it might look like the projectile isn’t heading in the right direction in third person, the direction is actually correct. If you try the same in first person, you will most likely notice that the projectiles are going exactly where you want them to go.

1 Like

Ah I see, is there any way I can take prespective into the equation?

I recommend just using a shorter range for the raycast. 1012 studs is excessive, as you don’t have any targets that far. 5000 would most likely be sufficient.

1 Like

To further illustrate the situation, here’s a short video showing how the projectile’s angle changes as the distance to the target increases. As you can see, at close range it’s obvious that the projectile is heading in the correct direction, but as the target gets further away, the angle starts becoming more subtle. The projectile is still heading in the right direction, even though it seems like it’s just going directly to the right.

Yea I know that but is it possible to change it so the projectile is headed in a different path based on the prespective of the player so instead of going like it did (arrow on the left) it’d go like the arrow on the right


I mean I’m at the point of giving up and just using walls, you sure there isn’t anything I can do?

You can also decrease the length of the mouse distance as @royee354 said.

Decreasing the ray’s reach doesn’t seem to fix it

image
Can I replicate this without using walls?

The walls function just to limit the ray length.

So my original point still stands, you could also limit the ray size to the size of the map in your case it looks like 100-200 studs.