i have a rotated cube, and a part. All i want to do is to stick the part to the closest face of the cube oriented kinda the same way as before. And i have no idea how to get the angles to rotate the part
Im guessing you already have raycasting in place and also positioning? What you could do to get the angle to rotate the part is to use the Normal of the RaycastResult, which returns the vector of the face intersected
hmm… how to say it. The part may also be rotated so i don’t know how can i get the angle of that. And yes I did the raycasting and already got the normal and distance to the closest face. but i didn’t get the part’s relative position to the cube
Cfram.new can be made up of 2 vector 3s (position, direction), or 3 numbers representing X,y,z positional offset.
In the code I suggested above, I’m setting p2’s position and rotation to p1’s position and rotation, then pushing it forward half the length of P1 and P2 so the faces are against eachother.
If you wanted to do this manually, you’d need to enable snapping it ask a builder how to do this.
but the problem is the angles and not the position. If i’m using P1’s CFrame then it might be looking backwards ((p21 = part before it was rotated, p22 = part after it was rotated)the projection of rotated part’s(p22) look vector onto the p21’s look vector might be opposite to the non rotated part’s (p21)look vector)
i tested it but it won’t work, since it will stay with the same orientation regardless of the part’s face that was hit. If the top is hit it will stay oriented as the cube, if the right is hit it will stay oriented as the cube and so on.
first you must choose which face your part should be sticked to and its offset. Then you just do the operations with CFrames.
local part = workspace.Part
local target = workspace.Target
local targetFace = -target.CFrame.UpVector -- this is the lower face
local offsetFace = target.Size.Y/2 -- for the lower face the displacement will be in Y
part.CFrame = CFrame.lookAt(Vector3.new(), targetFace)
part.CFrame += target.Position
part.CFrame *= CFrame.Angles(-math.pi/2, 0, 0)
part.CFrame *= CFrame.new(0, offsetFace + part.Size.Y/2, 0)