I want to set the part’s position limit. I want it to only go 10 studs away from the point. I have part A and B. I made part A as a middle point relatives to the part B. I want to move part B at X and Z directions in only 10 studs from part A. I tried many times but fail. I look every solution as I can but I’m still struggling. I hope people would understand my explanation
Hey, if I’m understanding correctly, you’re trying to move a only 10 studs in the direction from point A (part A) in the direction towards point B (part B). This can be achieved using unit vectors.
Here’s some example code:
local PartA = workspace.PartA
local PartB = workspace.PartB
local MovingPart = workspace.MovingPart
local PointA = PartA.Position
MovingPart.CFrame = CFrame.new(PointA + (PointA - PartB.Position).Unit * 10)
Does this also work with magnitude as well? I want to make this a loop. I only want part B to move. I’m trying to make moving a part by moving my mouse with it.
I do not think that I get It correctly what you are trying to say but I think this might help you!
place in a ScreenGui or some other context where it can interact with the player’s mouse.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local partA = workspace.PartA
local partB = workspace.PartB
-- The maximum distance B is allowed to be from A.
local maxDistance = 10
-- Run this function whenever the mouse moves.
mouse.Move:Connect(function()
-- Get the 3D position of the mouse in the world.
local mousePos = mouse.Hit.Position
-- Calculate the desired position for B relative to A.
local relativePos = mousePos - partA.Position
-- Ensure that this position is not more than maxDistance studs away.
local clampedPos = relativePos.Unit * math.min(relativePos.Magnitude, maxDistance)
-- Update the position of B.
partB.Position = partA.Position + Vector3.new(clampedPos.X, partB.Position.Y, clampedPos.Z)
end)
Thanks alot! I think I got the one I’m looking for. I have more further question. About the clamp thing, what does math.min really do?
math.min
is used to ensure that the distance of partB
from partA
doesn’t exceed maxDistance
I see. Also is Unit and Magnitude the same thing? Like what are the differences?
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.