Limit the Part's distance from player to 250 while maintaining mouse position correctly?

So I was trying to make a part follow the mouse and limit itself to 250 by multiplying a Unit by 250. However, there are some inconsistencies that I am not sure why occur.

local origin, direction = RepStorage.Functions.GetMousePos:InvokeClient(Player)

local Raycast = workspace:Raycast(origin,direction.Unit * 250,Params)

if (Raycast) and Raycast.Instance then

	newVFX.AimPart.CFrame = CFrame.new(Raycast.Position)

else

	newVFX.AimPart.CFrame = CFrame.new(direction.Unit * 250,origin)					

end
RepStorage.Functions.GetMousePos.OnClientInvoke = function()
	local Loc = UIS:GetMouseLocation()
	local unitRay = camera:ScreenPointToRay(Loc.X,Loc.Y)
	return unitRay.Origin,unitRay.Direction
	
end

Top is server, bottom is client. I am not too familiar with raycasting, so I am wondering why the mouse trails behind.

Video:

Thanks in advance.

I’d recommend using the Player’s Mouse for the mouse’s X and Z position as the UserInputService’s method is very incorrect (I think it has to do with the topbar and screen safe zones).

Try using this function that I wrote the other day:

function MouseCast(filterArray,distance)
	filterArray = filterArray or {}
	distance = distance or 1000
	if typeof(filterArray) == 'Instance' then
		filterArray = {filterArray}
	end
	local player = game:GetService('Players').LocalPlayer
	local mouse = player:GetMouse()
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	table.insert(filterArray,player.Character)
	params.FilterDescendantsInstances = filterArray
	local screenPoint = workspace.CurrentCamera:ScreenPointToRay(mouse.X,mouse.Y)
	local raycast = workspace:Raycast(screenPoint.Origin,screenPoint.Direction*distance,params)
	filterArray,distance,player,mouse,params,screenPoint = nil
	return raycast
end

Both arguments are optional. filterArray can be either an array of instances or just a single instance without an array. distance would be the maximum raycast distance, with a default max of 1000 studs.

Actually I see your issue, I didn’t realize that a raycast will just not return anything if nothing is found. I’ll make a change to fix that.

Yeah, the raycasting part was fine but the inconsistencies appear when there is no raycast instance.

Alright, try with this:

function MouseCast(filterArray,distance)
	filterArray = filterArray or {}
	distance = distance or 1000
	if typeof(filterArray) == 'Instance' then
		filterArray = {filterArray}
	end
	local player = game:GetService('Players').LocalPlayer
	local mouse = player:GetMouse()
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	table.insert(filterArray,player.Character)
	params.FilterDescendantsInstances = filterArray
	local screenPoint = workspace.CurrentCamera:ScreenPointToRay(mouse.X,mouse.Y)
	local raycast = workspace:Raycast(screenPoint.Origin,screenPoint.Direction*distance,params)
	if not raycast then
		raycast = {}
		raycast.Position = screenPoint.Origin + (screenPoint.Direction*distance)
		raycast.Distance = distance
	end
	filterArray,distance,player,mouse,params,screenPoint = nil
	return raycast
end

This will return basically a fake raycast result which should work fine as a replacement. It will only return a position and distance, so you can check by seeing if it has an instance or not, or just use the position only if that’s all you need.

Thank you with your help so far; I am off to bed. However, something I noticed is that it seems to ignore instances now or not return a position when it is hovered over. (raycast.Position should have intersect point if it exists)

Are you filtering the right instances? It also filters any descendants of instances you put in that array. I don’t seem to have that issue with my own testing, so I’m not sure what’s going on there.

I’ll check in the morning. I believe I am only filtering the VFX.

That part you highlighted is just returning the position at the maximum distance. This would only apply if the raycast failed, which means it wouldn’t have any intersecting parts.

Sorry, forgot to make this as the solution, tested it out and it worked fine. (Forgot to send some things to the server).

1 Like

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