How to rotate part according to attachments orientation

Im making a placement system where if the cursor is close to an attachment then the part locks onto it but 3 out fo 4 of the floors don’t align properly how would I fix that?

https://gyazo.com/7eac00a1c302b6e374c8e37a250addcb

Whatever I’ve tried so far hasn’t worked.

Here is the main part of the code where I think I could so something with to fix this problem:

            local floor = mouse.Target
            local floorStructure = clientStructure
            local floorCframe = floor.CFrame *CFrame.new(ClosestAttachment.Position)
            floorCframe = floorCframe *CFrame.new(-floor.FloorAttachment.Position)
             floorCframe = floorCframe *CFrame.Angles(0, math.rad(ClosestAttachment.Orientation.Y), 0)
            floorStructure.CFrame = floorCframe
            FloorPos = floorStructure.Position
            clientStructure.CFrame = floorCframe

Hmm, have you tried using an Attachments Axis and Secondary Axis and inputting that into CFrame.fromMatrix to achieve the desired rotation?

Otherwise, how have you set your attachments Axis, do the four attachments have the same axis which is why they have the same orientation for all four cases in the gif?

This is the code that deals with the rotation and it depends on the attachments orientation which isn’t provided in the question sorry.

Additionally, you want to try to use World orientation just in case.

Sorry for no code examples I’m on mobile.

I set up the orientation of the attachment so that the parts should face the right way but it still doesn’t work.

Hmm, then when working with CFrames for debugging I suggest applying the CFrame operations one at a time so you know what exactly happens when you multiply a CFrame with another CFrame.

this is what I have done for my IK project

I would like to see a gif of what happens if you remove this piece of code relating to the rotation of the floor which seems to be incorrect and causing the problem.

After that maybe we can work out the correct CFrame rotation needed to achieve the solution.

Edit: please note with CFrame multiplication the order it happens matters as it’s not commutative.

So I should remove this piece of code?

floorCframe = floorCframe *CFrame.Angles(0, math.rad(ClosestAttachment.Orientation.Y), 0)

and send you a gif of what happens?

I removed it and nothing changed at all

Hmm, maybe let’s try the earlier Attachment axis approach to achieve the desired rotation, to do this we construct the axis to get the right vector and up vector of the floor:

This piece of code will change the floor CFrame according to how the attachments axis is set.

This uses CFrame from matrix which can be explained in this thread

--Put floor at attachment position via CFrame
local floorCframe = floor.CFrame *CFrame.new(ClosestAttachment.Position)

--Achieve goal rotation CFrame using axis from attachments
--We put emptyVector because we don't want to add position into the CFrame from matrix
--Only rotation using the upvector and right vector
local emptyVector = Vector3.new(0,0,0)
local attachmentWorldUpvector = ClosestAttachment.WorldSecondaryAxis
local attachmentWorldRightvector = ClosestAttachment.WorldAxis

local goalRotationCFrame = CFrame.fromMatrix(emptyVector,attachmentWorldRightvector,attachmentWorldUpvector)

--apply rotation to floor rotation
floorCframe = floorCframe * goalRotationCFrame

--Then offset the position of the floor CFrame
floorCframe = floorCframe *CFrame.new(-floor.FloorAttachment.Position)

3 Likes

It somewhat worked now if I change the rotation of the attachment then it works fine but if I keep trying to build some weird stuff happens.

https://gyazo.com/6de54520a616b5851438d627ed88ce55

Hello please respond I’ve gotten really far and want to finish the building system up.

Sorry, but I don’t have enough information on why this weird stuff happens.

I would need to know the axis of the attachment, the cloned parts attachment axis as well and what is exactly happening at that moment.

Also when I rotate the floor then the orientation breaks

https://gyazo.com/66c83f2600d127080ecd366ae851df0d

Here are the attachment orientations:
https://gyazo.com/a807aa231a866f7bfeec2be624a2b8ba

Honestly I have no idea.

Maybe Instead of this:

try using this:

local attachmentWorldUpvector = ClosestAttachment.SecondaryAxis
local attachmentWorldRightvector = ClosestAttachment.Axis

Maybe its an issue with the rotation being in world terms and object terms scenario.

3 Likes

That worked! Thank you so much you saved me from so many problems!

Omg tysm i also had this problem. i was near to give up my entire survival game

1 Like

I know this is probably old and you might not reply, but what do i replace in the script you provided to put this script in? Do i just need to add this script?