Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

CFrame.Angles not going back to their original position?

Asked by 9 years ago

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?

0
oops you did not put your code in lua block correctly davness 376 — 9y
0
yeah i fixed that now jimmie9999 10 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
0
Thanks! Amazing answer! jimmie9999 10 — 9y
Ad

Answer this question