Is it possible to flip an animation player that deals with animating both position and rotation?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By OiKeTTLe

In the game I’m making, the player holds a sword (a separate scene) which uses an animation player to animate a swing. It works fine on the right side, and it changes both position and rotation fine when I move to the left, but when I trigger the animation, it still plays on the right. Anyone know a way around this? Help would be appreciated.

:bust_in_silhouette: Reply From: Yuminous

Overview

AnimationPlayer animates Node properties from key values of your choosing.
These values will remain the same even if the Node is being extensively modified in your script, so your problem as an example:

Undesired Animations
Notes about this answer for others — I have run this project’s code

Solution

While the AnimationPlayer will always set the animation to the same values, if you happen to flip the parent of the animated Node by setting its X axis scale to -1, from the animation’s perspective it is still achieving its desired rotation but from the main scene’s perspective, it looks like this:

Flipped Parent

In Practice

In this case, the Sword is an instanced scene which has its own animation and is the child of the Player scene.

But since flipping the Player in this manner is undesirable, what you can do instead is add a new Node2D in the Player scene and make the Sword instance a child of it:

SceneTree
Naturally after doing this you will have to change all references to the Sword node to be Node2D/Sword — though in your case you have direct references to the “PlayerOne” parent (player node), so change the name of Node2D to PlayerOne and change the references to PlayerOne/Sword.

Rename the Node
In order to flip the Node2D, you will need to also add to the Player movement script:

$PlayerOne.scale.x = 1 (on right movement)
$PlayerOne.scale.x = -1 (on left movement)

And you’ll also need to remove the code that was changing the Sword’s idle rotation:

Sword.position.x = -15
Sword.rotation_degrees = -9.4

From the left movement specifically, but the code in the right channel will become redundant as well.

And for future reference, the alternative option to all this (as you had implemented as a stop-gap) is to simply create a 2nd set of reversed animations, but at some point this would become impractical to maintain.

Final Result

The Desired Result
I hope this finds you well!
Good luck!!

Awesome, thanks! Really well structured and thought-out, I, and anybody else that finds this answer, appreciate it!

OiKeTTLe | 2021-07-15 13:50