Sprite moves during animation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zero
:warning: Old Version Published before Godot 3 was released.

I’m using a sprite sheet to animate a character, using the Sprite node and the Vframes, Hframes properties.

It seems to work well, but the sprite physically moves (changes its position) during the animation, and that’s not good because the sprite will also move during the game (the sprite will be at a slightly different position to the kinematicbody2d parent node).

(It should always be in the center)

For reference this is the image that I’m using for the spritesheet (996 x 239). Can anyone help?

:bust_in_silhouette: Reply From: splite

Well,

its all about that texture.

996(px) ÷ 9 (frames) = 110.66 px (lets say 110 px for simplicity)

If i add line every 110 px (so frame every 110, 220, 330, 440, 550, 660, 770, 880, 990), you can clearly see whats wrong (dont mind last line cutting his face, thats that .66 px × 9 frames we ignored)

Maybe author of that spritesheet want it to look like walking without moving sprite (thats what you got :slight_smile: ), idk. Dont think there is any technique to “multiplying offset every frame” and who know if that offset is distributed equally, i would scratch this idea.)

You have to pack that texture “more tightly” (draw lines as i did, resize texture to width ÷ frames = whole integer, but add at least two transparent pixels between sprites and one on every side of image*) or split it to multiple images and use AnimatedSprite.


Edit: or, you can use “region” and animate “region rectangle” property of your sprite (but i think repacking texture is little bit easier)

Edit2: *: so you will have padding and margin. Its becouse coord on textures is float2 between zero and one (lets say 0.123456789; 0.987654321) - which doesnt have to land exactly on pixel (lets say, texture is 90 px wide and you want pixel 33 → 33 ÷ 90 = 0.366666period…) and you got “half pixel” which, if neighbouring pixel is colored, will make artefacts on sides of your sprite, but thats heavy OT :slight_smile:

Ah, so it really is a problem with the image, thanks. I’ll try adjusting it or just use AnimatedSprite. Thank you very much for your detailed answer!

Zero | 2016-08-02 16:12

You are welcome :slight_smile:

splite | 2016-08-03 08:02