How to make a part face the closest side of another part?
Asked by
6 years ago Edited 6 years ago
Alright, so before beginning this post:
this is absolutely not the most efficient way to do this
i want to understand why THIS method is not working, not for a more efficient method
this project does NOT include the top and bottom surfaces of the init part
Alright so:
I have a part which I will drag around my Baseplate - and a reference part which stays in a single position on the Baseplate. I don't want to use cf.new(vector,vector) at the closest side - I just want it to FACE the closest side, not the middle of the closest side.
Currently this is what I have, and I feel the issue lies in my handling of rotations toward the end:
01 | local init_part = workspace.Init |
02 | local ref_part = workspace.Ref |
06 | local sub_x = init_part.Size.X / 2 |
07 | local sub_z = init_part.Size.Z / 2 |
09 | local two, four = init_part.Position + Vector 3. new(sub_x, 0 , 0 ), init_part.Position - Vector 3. new(sub_x, 0 , 0 ) |
10 | local one, three = init_part.Position + Vector 3. new( 0 , 0 ,sub_z), init_part.Position - Vector 3. new( 0 , 0 ,sub_z) |
12 | function determine_closest_side(from_part) |
13 | local one_dist = (from_part.Position - one).Magnitude |
14 | local two_dist = (from_part.Position - two).Magnitude |
15 | local three_dist = (from_part.Position - three).Magnitude |
16 | local four_dist = (from_part.Position - four).Magnitude |
18 | local distances = { one_dist, two_dist, three_dist, four_dist } |
21 | local closest = distances [ #distances ] |
23 | if one_dist = = closest then |
25 | elseif two_dist = = closest then |
27 | elseif three_dist = = closest then |
35 | local cframe_construct = CFrame.new(init_part.Position, determine_closest_side(ref_part)) |
37 | local part = Instance.new( "Part" ) |
38 | part.CFrame = cframe_construct |
41 | local y_orientation = part.Orientation.Y |
42 | local solved_orientation = y_orientation - math.pi |
44 | ref_part.CFrame = CFrame.new(ref_part.Position) * CFrame.Angles( 0 ,solved_orientation, 0 ) |
If you want to experiment with this:
1) Make two parts in Workspace
2) Name one part Ref, and the other part Init
3) Put this script in SSS
4) Drag Ref around the Init part in Run mode.
Hopefully this is a decent explanation - but if you have further questions just make a comment.