I want to find corners of part but I dont know how. So I have a part in workspace and I know its position and size, and I need to find 8 corners positions like Vector3 values, but position and size are vector3 too. This is my part (the red and blue circles are corners I need to find positions of):
On the solution, see you can see that you can get one corner by using:
local Size = Part.Size
local CornerPos = (Part.CFrame * CFrame.new(Size .X/2, Size .Y/2 , Size .Z/2)).Position
That will give you one corner so all you have to do is indicate where each corner using a table!
After you just need to multiple each vertex by the corresponding value:
for _, Vector in pairs(Vertices) do
local CornerPos = (Part.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2
Vector[3])).Position
end
If you wanted to get/find the bottom front right corner, then you would multiply the top front right corner (the corner got from doing local CornerPos = (Part.CFrame * CFrame.new(Size .X/2, Size .Y/2, Size .Z/2)). Position, by that. So if the top front right corner had a position of 10, 10, 10, then the bottom front right corner would have a position of 10, -10, -10!
Do you have any idea on how I wrap it into a function ? Like: function getcorners(part) … end
Because I need it to be a function returning a table with corners, so I can take a corner key from table and get its position as value…
I dont understand it it is complicated. Basically, I want just a simple function that returns a table, so i can use keys as corner names to refrence vector3s.
I probably didnt make it clear , sorry. You dont have to solve it for me though.
As of right now I almost found a solution myself, it involves lookvector/upvector/rightvector. I will add them to part’s cframe and multiply by value of part’s half size on that axis to get part’s face center. Then do this 2 more times but with other vectors and axes and so I get to my target corner. Now I just need to repeat it for every corner but in different order.
I understand what your question is. The answer I linked gives you the exact code you need, though. You can put it in a function if you want:
local Vertices = {
{1, 1, -1}, --v1 - top front right
{1, -1, -1}, --v2 - bottom front right
{-1, -1, -1},--v3 - bottom front left
{-1, 1, -1}, --v4 - top front left
{1, 1, 1}, --v5 - top back right
{1, -1, 1}, --v6 - bottom back right
{-1, -1, 1},--v7 - bottom back left
{-1, 1, 1} --v8 - top back left
}
local function GetCorners(Part)
local corners = {}
for _, Vector in pairs(Vertices) do
table.insert(corners, (Part.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2 * Vector[3])).Position)
end
return corners
end
Then call GetCorners to get a table of the corners. Again, credit to @VegetationBush.
There is a special function of CFrame called CFrame:PointToWorldSpace()
Basically it transforms the entire world to the part. This makes it easy to get Position relative to the part.
Also, this is if you want to get a Direction relative to the part CFrame:VectorToWorldSpace()
-- Pro tip: if you find yourself repeating code there is probably a better way of writing it.
local FRT = Part.CFrame:PointToWorldSpace(Vector3.new(Part.Size.X/2,Part.Size.Y/2,Part.Size.Z/2)
local Vis = Instance.new("Part",workspace)
Vis.Size = Vector3.new(0.1,0.1,0.1)
Vis.Position = FRT
@sktvladimir535 No, only cubes. However, it does support CFrame translations such as Position and Rotation. Please do some testing yourself before you bump a 3-year-old post.
Also, I want to thank you, @koziahss because this helped me out tremendously with one of my projects. Thank you so much <3