Setting a part's position inside a range

All right, basically what I am thinking of achieving is at some point control position with magnitude. Yes, magnitude is a read-only however hear me as I really don’t know what to do.
Assuming P1 is a point where it is the center of P2. What I would like to achieve is to check if the studs between those points are always below or equal to let’s say for 20 studs. P2 is always moving and the script is always checking for the new position of P2. Now assuming PS exceeds the studs we are looking for (20), is there any way to keep P2 in the 20 studs zone forcing it not to exceed it? Take World of Magic for example, at some point there is a type of explosion where you can manually place it, it has a system where you can’t move the spell beyond 20 studs however you can still control it inside with your mouse (Controlling the Z or probably X). Any idea or suggestion is appreciated!

I’d check the .Magnitude between P1 and P2, using RunService to check where they’re at per frame. If P2 exceeds the limit of 20 (if magnitude >= 20) then I’d use break for it to stop moving, or however you set it up as.

1 Like

That’s what I thought too, but the issue I couldn’t find a solution for is that the user is the one who is choosing where to place P2. Assuming the magnitude is larger 20, is there any way to transfer P2’s position enough so it matches 20 studs?

I think that’d require CFrames in which I’m not fully aware of how it functions, but you could check if it exceeds X, Y, or Z axis, then position it by 20 studs using Position at that axis.

1 Like

I’ll do that; thanks for replying!

For anyone who’s looking for this solution, a friend of mine suggested it and it worked:

if (P1.Position - P2.Position).Magnitude >= 50 then
	P2.Position =P1.Position + (P2.Position-P1.Position).Unit*50
end
2 Likes