0 votes

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.

Godot version 3.2.3
in Engine by (77 points)

1 Answer

+1 vote
Best answer

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!!

by (992 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.