-
What do you want to achieve?
I’m currently using:
local function ClosestPointOnPart(Part, Point)
local Transform = Part.CFrame:pointToObjectSpace(Point) -- Transform into local space
local HalfSize = Part.Size * 0.5
return Part.CFrame * Vector3.new( -- Clamp & transform into world space
math.clamp(Transform.x, -HalfSize.x, HalfSize.x),
math.clamp(Transform.y, -HalfSize.y, HalfSize.y),
math.clamp(Transform.z, -HalfSize.z, HalfSize.z)
) * Vector3.new(1,0,1) + (Vector3.new(0,1,0) * Part.Position)
end
To calculate the closest point on one part in relation to another part’s position. Please ignore the extra vector3s after the math.Clamp() (I want it to return a position at a constant height).
2. What is the issue?
Now, some parts that I use this formula are not rectangular. I’ve got a few sideways cylinders and wedges (essentially circles and triangles because I lock the Y Axis). Now, this formula assumes that they are rectangular, which leads to this:
As you can see, the closest point (as I placed them far away) is at the corner. Unfortunately, the parts are not actually touching.
3. What solutions have you tried so far?
I have attempted Raycasting, but to no avail. I have also browsed the forum for solutions.
In conclusion, my current formula does not clamp in the way that I want it to (either in a circular fashion or a triangular fashion as well as a rectangular fashion. I will know which.) How can I clamp like this?
If this does not work, I am happy to use different solutions like Raycasting. If so, please explain to me how those solutions work.
The Developer Forum has become invaluable as I progress with my development. Thank you all for your time and ideas!