Offset in AnimationPlayer not working as intended

Godot Version 4.3

Hi there!

I’m currently in a test environment trying to create a 2D platformer/fighting prototype where input direction + attack key will output a certain attack movement. I use Sprite2D and AnimationPlayer for my animations.

Where I am struggling right now is adding offsets in the attack animation of the animationplayer which would allow for flip h, and for the character’s ending position to stick rather than reset when state changes.
The attack motion has 7 key frames (we’ll call them F1, F2 … F7 to represent each frame). At exactly F5, the character makes a step forward, so in the animationplayer I have applied an offset on the position x of the Sprite2D and CollisionShape2D by value 15 for F5, F6 and F7 (as shown in the images below).


The issues faced are:

  1. When the character is facing left and flip h is applied, the offset would still happen in the +x direction rather than following the character’s direction.
  2. When the character finishes the attack motion, given the offset in the animationplayer, the Sprite2D and CollissionShape2D should have been offset by 15. Instead the location resets once the animation ends, i.e. it was like the offset did not happen.

Not sure what a good fix would be for both, honestly the only thing I could think of was maybe to create some kind of signal to emit during the animation, which would then get caught by the process/physics_process to then manually offset by the direction * offset value.

Thank you for reading up to this point! Any thoughts, advice or even solutions which you have implemented would be much appreciated :slight_smile:

Potential work arounds were discovered after having thought about it overnight with the help of a few more videos.

For problem 1, Area2D doesn’t seem to work with the flip_h even if you make it a child of Sprite2D. The work around was to just create an export var for Area2D, and in the flip_h logic add a new line to Area2D.scale.x = direction, where direction = input.get_axis. This allows the Area2D to scale to the negative, which allows it to flip.

For problem 2, a timer was created. The position.x * direction of the player character was then shifted when the timeout happens. There is still the issue where direction is determined by input.get_axis, so if the arrow keys are not pressed the offset won’t happen. I think this could probably be resolved by a storing the value and using the stored value instead.

Quick note on the offset, if you are not careful the player character moves into another object and then bounces out. When I was testing I had an enemy character in the air. The offset caused the character to move into the enemy, such that the collision shape overlaps, I think in the next frame the player character “bounces out”, where in my case it got displaced downward. This caused the character to glitch into the floor below.

It’s probably me not setting something properly, but if ever a teleportation skill type thing is implemented and the formula used is
position.x = position.x + offset * direction,
may have to think about how to handle “teleporting” into things.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.