Maximum mouse.hit.p is around 10000

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? i want to create a real scale tank simulation

  2. What is the issue? when I minus the launching pos from the mouse.hit.p,The .magnitude returns around 10000,While the real distance of the building that im shooting is around 12000 stud,When i print it it return as 10000 instead,Even when use a custom .Magnitude function

  3. **What solutions have you tried so far?**Firstly,When the real …Magnitude dont work,I’ve created a new function which does math.abs on xyz and add it up,And also seems that the raycast has limit,Idk is this matter or not

heres the script

local d = abs(hrpcf * Vector3.new(0, 2, -2) - pos)

Custom magnitude func

local function abs(pos)
	local x = math.abs(pos.X)
	local y = math.abs(pos.Y)
	local z = math.abs(pos.Z)
	return x+z+y
end
3 Likes

Well, as you’re saying, it’s probably the maximum yeah. What you could try to do is use ScreenPointToRay() to remove the limit. I’m not sure if this works but you can try.

1 Like

Just to confirm this, yeah there’s a maximum length for every raycast:

The mouse’s internal ray extends for 1000 studs. If the mouse is not pointing at an object in 3D space (for example when pointing at the sky), this property will be 1000 studs away from the Workspace.CurrentCamera .
- Mouse | Roblox Creator Documentation

and somewhat related

The maximum length of the direction vector is 5,000 studs
- WorldRoot | Roblox Creator Documentation

Annoyingly the wiki says 1000 when OP is clearly getting a result of 10,000 :-/
@tin_nim please double check your results and post in this wiki correction thread:

It technically specifies that factually incorrect API docs don’t belong there, but only regulars can post actual bug reports so unless you’re regular it’s your best bet AFAIK :frowning: At least this thread will turn up when other people have the same issue.

1 Like

To work around the limit, you can create a function that chains together several “small” raycasts to form one long raycast:

local RAYCAST_MAX_DISTANCE = 5000

function longRaycast(origin: Vector3, direction: Vector3, raycastParams: RaycastParams): RaycastResult
    local result = game:GetService("Workspace"):Raycast(origin, direction, raycastParams)
    if result then
         return result
    elseif direction.Magnitude > RAYCAST_MAX_DISTANCE then
        return longRaycast(origin + direction, direction - direction.UNIT * RAYCAST_MAX_DISTANCE)
    else --This one strictly unnecessary, you can remove it if you understand why the function would return nil either way
        return nil
    end
end

I haven’t tested this to see if there are any errors, but the idea works. One thing I’m not sure about is what happens if a surface on the long ray is exactly between the end of one short ray and the start of the next short ray. I.e. if a surface doesn’t get detected if the ray is cast from a point on that surface or if the ray ends exactly at that surface. Testing is needed to find out. If it’s an issue, an easy work around is to make the rays slightly overlap.

Hope this helps!

2 Likes

I have been summoned! Readers – you can message me directly with Developer Hub corrections if you are otherwise unable to post in that topic/megathread. I implore you to make my job easy by providing some code to demonstrate the actually correct behavior. I’ll be more than happy to make the correction :slight_smile:

1 Like

Hello,It actually works I’ve made some modification to it to make it work with mouse.hit
Raycasting Code

local function longRaycast(origin: Vector3, direction: Vector3, raycastParams: RaycastParams): RaycastResult
	local result = game:GetService("Workspace"):Raycast(origin, direction, raycastParams)
	if result then
		return result
	elseif direction.Magnitude > 4500 then
		local left = direction.Magnitude -4500
		print("!")
		return longRaycast(origin + direction, direction - direction.Unit * left,raycastParams)
	else --This one strictly unnecessary, you can remove it if you understand why the function would return nil either way
		return nil
	end
end

Mouse Detection Stuff

local unitRayDirection = mouse.UnitRay.Direction
local rtx = longRaycast(script.Parent.Handle.Position,unitRayDirection*12000,raycastparam)--12000 is the distance,Max