I need help trying to calculate the positions so that my placement part will stay on the plot. I do not know how to calculate this using positions.
I do not know if Region3 is the best way? I do not know how to use it well.
I need help trying to calculate the positions so that my placement part will stay on the plot. I do not know how to calculate this using positions.
I do not know if Region3 is the best way? I do not know how to use it well.
You want to clamp the position of the placement part. Using math.clamp(x, minPosition, maxPosition) you can force the part to stay on the plot. math.clamp doesn’t allow the inputted x value to be anything lower than minPosition or anything higher than maxPosition.
You can’t clamp its entire Vector3 but you may clamp each individual value of the Vector3, e.g. the x and z values.
You want to clamp the part’s x and z position values to the bounds of the plot.
I think this works as a function to clamp Vector3s.
local function clampVector3(vector, min, max)
local newMagnitude = math.clamp(vector.Magnitude, min, max)
return vector.Unit * newMagnitude
end
One note, though: If the vector is Vector3.new(0, 0, 0) then doing .Unit on that will turn it into Vector3.new(NAN, NAN, NAN), which you can’t then multiply by the magnitude. So you have to check for that as well.
What is the min and the max for? And how do I put this into a if statement?
You don’t use a conditional statement for this.
Here’s an example of math.clamp:
local function clampNumber(num, min, max)
local clampedNumber = math.clamp(num,min,max)
print(clampedNumber)
end
clampNumber(4,10,15)
Output:
10
Because the number to clamp was less than the min, it returned the min.
Other possibilities:
PS: While @Zephyr_42 has a function that clamps the Vector3, you will want to clamp the x and z values, while locking the y value entirely in order to keep the part from moving up and down the Y axis.
I figured it out. Thanks for your help!
so this becomes your final function.
i apologize if there’s something wrong with it, I wrote it in a rush.
local function clampVector3(vector, min, max)
local _vector = vector.Unit
local newItems = {_vector.X, _vector.Y, _vector.Z}
local conditions = {_vector.X ~= _vector.X, _vector.Y ~= _vector.Y, _vector.Z ~= _vector.Z}
local clamped = math.clamp(0, min, max)
for i, condition in ipairs(conditions) do
if condition then
-- NAN ~= NAN is always true so we use that to check
newItems[i] = clamped
else
newItems[i] = newItems[i] * math.clamp(newItems[i], min, max)
end
end
return (Vector3.new(unpack(newItems))
* Vector3.new(1, 0, 1))
+ Vector3.new(0, vector.Y, 0)
end
it checks if a Vector3 is 0, 0, 0 which would cause another error, and it keeps vector.Y so Y doesn’t get clamped along with X and Z.
i just fixed two errors with it