How can i Achieve this?

I’m trying to get from this

https://gyazo.com/8c1a015abb3f26a3125466f82f3cd700

to this

https://gyazo.com/861ac5085c8f1f23280b91ef937f0fc2

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 :expressionless:

Thanks in advance,
Tem

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

P1 = workspace.BigBlock
P2 = workspace.OtherBlock


P2.CFrame = P1.CFrame * CFrame.new(0, 0, P1.Size.Z/2 + P2.Size.Z/2

P2.CFrame = P1.CFrame * CFrame.new(0, 0, P1.Size.Z/2 + P2.Size.Z/2)

what should this be?
Also CFrame.new is CFrame.new(vector3, vector3 and three more vector3)

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.

oh, so you say P2.CFrame = P1.CFrame * CFrame.new(vector3.new(0, 0, P1.Size.Z/2 + P2.Size.Z/2)) ? oops didn’t read the edit

i got it now

That should work equally.

Although as mentioned, there is an overload for that so you can just use numbers.

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)

Just add a rotation if it’s backwards…

  • CFrame.Angles(0, math.rad(180), 0)

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)