I’m trying to make a script that changes a part’s rotation to the orientation of the surface below it. But I have an issue. The part does what it has to do, but it also makes sudden turns from a specific axis and I dont know why that happens.
It’s a bit hard to explain what the exact problem is so I made a recording.
Recording:
Script:
function newPet(relativePart)
print("function ran")
local tank = workspace.Model
local testPart = tank.TestPart
local alignOrientation = testPart.AlignOrientation
rParams.FilterDescendantsInstances = {testPart, relativePart, workspace.Base}
while task.wait(1) do
local newRay = workspace:Raycast(testPart.Position, Vector3.new(0,-1,0) * 100, rParams) -- Raycast to get floor position and normal
if newRay then
local normal = newRay.Normal
local dotproduct = -normal:Cross(testPart.CFrame.RightVector)
local FinalCframe = CFrame.fromMatrix(
testPart.Position, -- POSITION OF THE OBJECT
dotproduct, -- LOOKVECTOR
testPart.CFrame.RightVector, --RIGHTVECTOR
normal --UPVECTOR
)
local newCFrame = FinalCframe
alignOrientation.CFrame = newCFrame.Rotation
end
end
end
If someone would help me, I would be very thankful, because I’m trying to fix this bug for more than 3 days now
nope exactly the same result, I prob should also note that when I use another part for the rightvector its works completely fine, but I want it to be independent and work on its own
Why not use the AlignOrientation the way it’s supposed to be used.
Have 1 Attachment in each Part.
Unanchor the Part you want to rotate and use an AlignPosition to keep that Part’s Attachment in one spot.
Use the AlignOrientation to join the 2 Attachments.
Rotate your first Part with the drag handles and watch the 2nd Part keep in alignment.
i actually had that, but is that necessary tho? I mean is there a way to make this work with one attachment instead of just using two attachments, cuz i dont want some random parts slingering around in the workspace
I think I know a solution, what i think the problem is that everytime the parts CFrame is adjusted the rightvector also gets changed and that results in these weird turns of 180 degrees, so how I would fix this is by setting a rightvector for each movement the part’s gonna make, what I mean by this is that, when I want the part to move the a specific position, im gonna set the rightvector at the begin of the movement to CFrame.new(startPos, endPos).RightVector and then im gonna use that rightvector for the whole movement, and because of that the rightvector remains unchanged the whole journey. Which should prevent the part of making these weird turns.
That’s what the AlignPosition takes care of.
After the part is aligned to the Orientation you want you can just Anchor it and delete the Attachments and Constraints.
Your other issue might be due to ‘gimbal lock’ when CFraming. For example when you rotate an item by 90 degrees (let’s say in the X axis) in certain types of CFraming that axis aligns with one of the other 2 (Y or Z). Now when you try to rotate it from that point the CFraming can get wonky because the original orientation doesn’t apply to the new orientation.
Search up ‘gimbal lock’ on the forums. There have been a few posts that explain how to fix it better than I can.
Alright I’ll do my research about gimbal lock, even though I fixed the problem, it might be useful in the future one day or to improve the currenty system I have rn, thanks for replying btw, I appreciate that.