I was working on an NPC, I can't afford animations so I used the old trick;
for i = 1,10 do RS.C0 = RS.C0 *CFrame.Angles(0,0,.08) RS.C0 = RS.C0 *CFrame.Angles(0,.04,0) RS.C0 = RS.C0 *CFrame.Angles(.04,0,0) wait(.01) end wait(1) for i = 1,10 do RS.C0 = RS.C0 *CFrame.Angles(0,0,-.08) RS.C0 = RS.C0 *CFrame.Angles(0,-.04,0) RS.C0 = RS.C0 *CFrame.Angles(-.04,0,0) wait(.01) end
RS means "script.Parent.Torso["Right Shoulder"]" However, when this command finishes, the arm is a bit off to the X angle. I couldn't seem to find any other people who had found this problem, does anyone know a solution?
The problem is that the order you do these things in matters a lot, since matrix multiplication is not commutative.
In order to "undo" an operation, it needs to be done precisely before the last. E.g., if x
and y
are the "forwards" and X
and Y
are the opposites (the undos), then x * y * X * Y
won't work -- you have to do x * y * Y * x
-- only then can the y * Y
cancel out.
Since you're doing
a b c a b c a b c ... a b c
, you have to undo with C B A C B A C B A ... C B A
That just means flipping the order of the three rotations in the second loop.
for i = 1,10 do RS.C0 = RS.C0 *CFrame.Angles(0,0,.08) RS.C0 = RS.C0 *CFrame.Angles(0,.04,0) RS.C0 = RS.C0 *CFrame.Angles(.04,0,0) wait(.01) end wait(1) for i = 1,10 do RS.C0 = RS.C0 *CFrame.Angles(-.04,0,0) RS.C0 = RS.C0 *CFrame.Angles(0,-.04,0) RS.C0 = RS.C0 *CFrame.Angles(0,0,-.08) wait(.01) end
Remember that wait(.01)
will actually be waiting at least .03 seconds!
A different way to approach this would be to simply save every CFrame and go backwards through them -- setting, not multiplying:
local history = {} for i = 1,10 do RS.C0 = RS.C0 *CFrame.Angles(0,0,.08) RS.C0 = RS.C0 *CFrame.Angles(0,.04,0) RS.C0 = RS.C0 *CFrame.Angles(.04,0,0) table.insert(history, RS.C0) wait(.01) end for i = #history, 1, -1 do RS.C0 = history[i] wait(.01) end