Limit Mouse.Hit.Position For Raycast

I have a raycast, and if it returns no instance, then the result is going to be the users mouse.Hit. The problem is that the distance beetween the firepos and mouse.hit.position is bigger than the max part size (2048) so how would i limit this?

local raycast = workspace:Raycast(Tool.FirePos.Position,(Mouse.Hit.Position-Tool.FirePos.Position)+Vector3.new(Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value),Random.new():NextNumber(-Values.Spread.Value,Values.Spread.Value))*1.2)
	local result
	local origin = Tool.FirePos.Position
	local target
if raycast then
		result = raycast
	else
		result = Mouse.Hit
	end

	local distance = (origin - result.Position).Magnitude
	local p = Instance.new("Part")
	p.Size = Vector3.new(0.1, 0.1, distance)
	p.CFrame = CFrame.lookAt(origin, result.Position)*CFrame.new(0, 0, -distance/2)

	
	p.Parent = workspace
	game.Debris:AddItem(p,.05)
1 Like

limit distance from firepos to Mouse.Hit.Position, how would i?

1 Like

ok actually Mouse.Hit.Position.Unit * 2000 worked

1 Like

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