Dragging Player around inside Square around the Player

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

  1. What do you want to achieve? Keep it simple and clear!
    Hello, I want to achieve that I can drag the Player around inside Square around the Player.

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    So far I tried scripting.

The scripts are all taking part inside this.
Place1.rbxl (60,1 KB)

The script

local Max_Distance = 6

local Connections = {}

local part = nil
local pos = nil

local mouse = game.Players.LocalPlayer:GetMouse()
local UIS = game:GetService("UserInputService")

game.ReplicatedStorage:WaitForChild("BroIsCookingSomething?")
game.ReplicatedStorage["BroIsCookingSomething?"].OnClientEvent:Connect(function(value,dragdetector : DragDetector )
	local player = game.Players.LocalPlayer
	local character = player.Character
	local head = character:WaitForChild("Head")
	if value then
		part = dragdetector.Parent:WaitForChild('AlignPosition')
		local boomer : AlignOrientation = dragdetector.Parent:WaitForChild('AlignOrientation')
	
		Connections[dragdetector] = game:GetService("RunService").RenderStepped:Connect(function()
			local MouseScreenPointPos = UIS:GetMouseLocation()
			local ViewportMouseRay = workspace.CurrentCamera:ViewportPointToRay(MouseScreenPointPos.X, MouseScreenPointPos.Y)
			local fakepos = ViewportMouseRay.Origin + ViewportMouseRay.Direction* (Max_Distance+(ViewportMouseRay.Origin-head.Position).Magnitude )
			local actualpos = head.Position+ CFrame.new(head.Position,fakepos).LookVector *Max_Distance
			if UIS:IsKeyDown(Enum.KeyCode.LeftControl) then
				pos = actualpos
			else
				pos = ViewportMouseRay.Origin + ViewportMouseRay.Direction* (Max_Distance+(ViewportMouseRay.Origin-head.Position).Magnitude )
			end
			boomer.CFrame = CFrame.new(dragdetector.Parent.PrimaryPart.Position,head.Position)
		end)

	else
		part = nil
		pos = nil
		if Connections[dragdetector] then
			Connections[dragdetector]:Disconnect()
			Connections[dragdetector] = nil
		end
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if part and pos then
		part.Position= pos
	end
end)
1 Like

Thanks to @EgoMoose Profile - EgoMoose - Developer Forum | Roblox

I’d argue that this is more of a UX problem than anything. You need to think of effective ways to communicate to the player that dragging the rat too far is not allowed so that it still “feels good” when they try.

However, if you’re simply looking for an answer as to how you could check if a point is inside a 3D rectangle vs a 3D radius check you could use a CFrame from the character and do something like:

local function isPointNearBox(boxSize: Vector3, point: Vector3)
	local characterCFrame = CFrame.new(humanoidRootPart.Position)
	local relativePoint = characterCFrame:PointToObjectSpace(point)
	return (
		math.abs(relativePoint.X) <= boxSize.X / 2
		and math.abs(relativePoint.Y) <= boxSize.Y / 2
		and math.abs(relativePoint.Z) <= boxSize.Z / 2
	)
end

Or if you’re trying to keep things screenspace you could use this:

With his help, I was able to create this script.

local function clampPosition(position, minVector, maxVector)
    local clampedX = math.clamp(position.X, minVector.X, maxVector.X)
    local clampedY = math.clamp(position.Y, minVector.Y, maxVector.Y)
    local clampedZ = math.clamp(position.Z, minVector.Z, maxVector.Z)
    return Vector3.new(clampedX, clampedY, clampedZ)
end

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