I want mouse to only be allowed to go within a circle

So I used clamps to keep the part from going too far out, but it does this weird slide thing and is a square instead of a circle. How would I make the “range” a circle?.
https://gyazo.com/fa51dc7f9969dccdaab66f220d9c2be6

Current Script:

game:GetService("RunService").RenderStepped:Connect(function()
	if mouse.Hit then
		local RootPos = c.HumanoidRootPart.Position
		local MousePos = mouse.Hit.p
		local camera = workspace.CurrentCamera
		workspace.MeshPart.Position = Vector3.new(math.clamp(MousePos.X,RootPos.X-30,RootPos.X+30),0,math.clamp(MousePos.Z,RootPos.Z-30,RootPos.Z+30))
	end
end)
1 Like

Couldn’t you use Magnitude?
<Mouse | Roblox Creator Documentation

1 Like

Hello again!

You could calculate the magnitude of the clamped position from the player/camera and move it towards the player till its below 30 studs. This sounds rather inefficient but hopefully it gets you in the right direction! :slight_smile:

This could work: Calculate the magnitude and subtract 30 (your distance) from it if its greater. Now, move the cursor towards the player that amount. This could solve your issue.

Example: Magnitude is 40, subtract 30 giving us 10. Move the cursor twards the player by 10 studs.

https://gyazo.com/dd6c8c1b156e7cfcf90144e1b6405b9c

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = game.Players.LocalPlayer:GetMouse()
local HRP = character.HumanoidRootPart

local radius = 30
game:GetService("RunService").RenderStepped:Connect(function()
	if mouse.Hit then
		local MousePos = mouse.Hit.Position
		local RootPos = HRP.Position
		
		local dif = (MousePos - RootPos)
		local mag = dif.Magnitude
		local unit = dif.Unit
		unit = unit + Vector3.new(0, -unit.Y, 0)
		
		
		if mag <= radius then
			workspace.MeshPart.Position = Vector3.new(MousePos.X, RootPos.Y, MousePos.Z) --Y can't go above the HRP
		else
			workspace.MeshPart.Position = RootPos + unit * radius
		end
	end
end)
2 Likes