Issue with simple rotation script

Hi. I am trying to make a basic swinging of a weapon/tool animation so I made a script attached to the player’s shoulder that SHOULD rotate the arm correctly then come back to the original rotation but for some reason, every time that i swing the tool, the tool ends up rotated a little more forwards than it was before swinging. I figured that the rotation forwards and backwards should cancel each-other out since they are the same rotation but with a negative magnitude and it is called the same number of times. I tried adding 1 to the number of frames it takes to rotate back to the original position to see if that would fix it but then it was rotating backwards a little too much each time. anyway, here’s the code, let me know where I’m going wrong if you spot it, I’m completely lost with this one:

I can’t see anything obvious with the code. Can you link to the project please?

sure. i made a new scene with this script being the only one in it yet the issue still occurs. here’s a link to the stripped down one:

I’ve done some further testing and I’m even more confused.

for the lines:
changing the first values in the rotateLocal functions to 0 will have the remaining rotation work perfectly and return to where is was meant to be. when I instead set the second values to 0 I found that the problem was happening again. it looks like its always the rotation with the x that is messing with it. I have tried setting the local rotation equal to 0,0,0 once the animation has finished but still it never returns all the way.

I’m guessing as I don’t know 100% for sure. As you are rotating along at least two local axis every frame, the local axis are constantly changing which is why it doesn’t end up back in the same rotation. So you rotate a bit and the local axis are now pointing in different directions so when you rotate locally again, it’s slightly different.

I’ve solved this in a very naive way here by setting the rotation directly:

Another alternative (and arguably better) is to have two quaternions, representing the start and end rotations and then slerp between them. This will allow you to use easing to for a better animation if need be.

1 Like

I tried the old code but made it only use a single local axis and it seems as though the issue is solely with the x-axis. The y-axis works fine on its own but the x-axis on its own produces the problem. It also seems that when both local axes are moved, the y axis returns to where it should be and it’s only the x axis that is getting thrown off. The reason I was using local is to have an easy way to keep the swing direction consistent with the orientation of the player. I could work around it using your method if i just add the player’s orientation into the calculation but it still seems odd that something so simple doesn’t work. Thanks for the help though yaustar, I’ll see about getting it working with your method, you were a big help.

Hmm… Doing it only on the X axis using your old code, I don’t get the problem you are describing:

Oops. Just change it to setLocalEularAngles. I’ve just updated the project to reflect that:

That’s because you are changing the orientation of the local axis every time you rotate locally along two axis therefore trying to ‘reverse’ the rotation the way you are doing it will lead to a different rotation path. It’s really hard to describe the issue over the forums as it kind of requires a prop to show you the rotation issue.