Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I keep moving part even if distance is too big?

Well its kind of hard to explain but I'm currently trying to find out how can I make part move even when magnitude between player and mouse is above 50, so it will move but not above 50 distance, right now if distance between player and mouse is 51 or more, part just stuck at the last point it was and doesn't keeps moving spherically

local plr = game.Players.LocalPlayer
local char = plr.Character
local hroot = char:WaitForChild("HumanoidRootPart")
local RS = game:GetService("RunService")
local mouse = plr:GetMouse()
local p = Instance.new("Part")
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(1,1,1)
mouse.TargetFilter = p
p.Parent = workspace

local function GetDistance(n1,n2)
	local distance = (n1.Position-n2.Position).Magnitude
	return distance
end

RS.RenderStepped:Connect(function()
	if GetDistance(hroot,mouse.Hit) < 50 then
		p.Position = mouse.Hit.Position
	end
	local mouse = game.Players.LocalPlayer:GetMouse()
	local camPos = game.Workspace.CurrentCamera.CoordinateFrame.p
end)

2 comments

0

The if statement should be under the variables

0

These variables aren't even attached to anything, I mean they aren't used to work with something, they're in it for later use, my question is other, everything works

1 answer

to do this, you need to get the "direction" (or just the difference between their positions, but normalized) from the HumanoidRootPart to the mouse's hit position

then, you can multiply that by either the actual distance between them or 50 to get the final position

RS.RenderStepped:Connect(function()
    local dirToMouse = (mouse.Hit.Position - hroot.Position).Unit -- (b - a) gives you b relative to a, and doing .Unit gives you that vector with a length of 1, or the ""direction""
    p.Position = hroot.Position + dirToMouse * math.min(50, GetDistance(hroot, mouse.Hit))
    local mouse = game.Players.LocalPlayer:GetMouse()
    local camPos = game.Workspace.CurrentCamera.CoordinateFrame.p
end)

edit: an admin supposedly "noted" this post and said i had to edit the answer but there was literally nothing on the page saying what i was supposed to edit because sh is a great website so i'm just going to leave it as is ????

1 comment

0

it does works tho but its not relative to mouse position, it is some kind offset?

This discussion is preserved as it was. New answers, comments, and votes are closed.