libgdx – Aligning animation frame assets with different sizes

I have code that changes texture in each frame depending on the player’s state – walking, idle.

My problem is that the idle texture (from separate png) has a slightly different size, and due to that it appears not in line with rest of the animations, seemingly being in a slightly different position when returning to idle.

How can I position the frames based on the center of texture instead of the bottom left?

batch.draw(determineTexture(), position.x, position.y,
            currentFrame.getRegionWidth()/2f, currentFrame.getRegionHeight()/2f,
            currentFrame.getRegionWidth(), currentFrame.getRegionHeight(),1f,1f, rotation,false);

TextureRegion determineTexture(){
        if(playerState == State.idle)
            currentFrame = idleAnimation.getKeyFrame(stateTime, true);
        else if (playerState == State.walkingUP || playerState == State.walkingDOWN || playerState == State.walkingRIGHT || playerState == State.walkingLEFT )
            currentFrame = walkAnimation.getKeyFrame(stateTime, true);
        return currentFrame;

One png has a height of 57 – idle, while 6 columns and one row of walking frames have a height of 87 pixels.

unity png sequence animation chaning position in animation

We are making frame by frame animation and we have a canvas size for animation. Attack animation is changing position in canvas. In unity after attack animation completed it teleports back to the initial position. I tried setting animation event and updating parent position but it does not teleports to nonsense locations like player is looking at +x but it teleports to -x. Anybody have solution for this ? since it is not rigged root motion is not working

unity – Gun clipping through arm during thirdperson animation)

I have been setting up a Blend Tree for motion animation in Unity. I’m using Root Motion for now. I parented the gun to the root of the player and then I use OnAnimatorIK to lock the two hands in position.

The overall looks and feel is great by my standards. The only thing I am disappointed about is the butt of the gun clips through the character’s upper arm when running.

Gun butt problem

Is there a way to crop this in the game? I’d like to avoid going into a 3d Editor and making it two separate meshes which I could then I guess somehow disable. I’m hoping there is a better way to handle this situation.

Also I wonder if the gun being parented to the Root player object is wise. I had tried parenting to the ‘right-hand’ bone but this made the gun point upwards about 45° and looked silly.

Why does Unity show dark regions on a model with animation created using Blender?

I sculpted a model in blender, and I want to import that to Unity. When I import just the model(without animation), Unity shows as is. However, when I attach armature to the body and automatically assign weights to it with the intention of creating an animation, Unity import shows dark regions all over the model. All the face normals look fine. I am not sure what the issue is.

Model in Blender:
Model in Blender:


Dark patches in Unity:
Dark patches in Unity

unity – Humanoid Retargetting: Number of frames in LateUpdate is not the same as frames in animation

I add a second answer since the first one is valid for others in similar situations.

Normally you don’t have to calculate the frame rate as you do – since it can be done even without playing in my previous answer. However, you are trying to figure out why not all frames were playing once the animation is “done” so I think I understand what you are trying to do. Unless some other parts of your code interfere with the “_iFrameCount”, it should be the same as the number of frames in your animation – unless (and here is probably the reason), your check at the end of the LateUpdate() is not doing what you expect.

if (_animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !_animator.IsInTransition(0))
    //animation has finished playing
    Debug.Log("Frames: " + _iFrameCount); //this returns 43 frames. Why???

When normalizedTime is greater than 1, you have reached the end of the current animation, which can consist of several animation clips like this:

enter image description here

It is first when we reach the end of the second animation normalizedTime > 1. Then, of course, why is “_iFrameCount” less than you expect. Well, could be several reasons, for example, this:

enter image description here

Note that the two animations will finish, normalizedTime > 1, before the first animation has finished (if it had not been interrupted by the second animation).

The animator could also be interrupted by some other code/event.

Efficient sprite animation display methods?

I’m making a game sprite, with animations for transitioning to different states, such as idle, sleeping, etc. I’m wondering what the most efficient way to play the animations is. As of now, I have a couple of ideas:

  • store all frames as separate PNG images, and display only the frames for the current state (e.g. play frames 1-5 for idle, 5-10 for transitioning, 10-15 for sleeping)
  • make the animations into GIFs and play the one corresponding to the current state.
  • merge them all into a spritesheet.

Please let me know which would be the best approach, or if there’s a better way to do things. I’m new to handling images and animation with code, so I’m not sure what the advantages and disadvantages are. Thanks!

In case the information helps anyone, I’m planning to use Python for this (although I could also do Java or C++, if there are good solutions in those languages), and the sprite image size isn’t large, it’s pixel art.

unity – Why does my in-game frame calculation not correlate with animation length?

I need to play an animation in such a way that each frame is for sure being played and not skipped.
To do that, I use a very slow animator speed like this:

_animator.speed = 0.01f;

The Unity editor says this about the animation:

enter image description here

To make sure that each frame is really shown / played, I have implemented my own counter.
My counter however returns 43 frames and not 51 frames as the Inspector.
No matter how often I repeat this process, it always return 43 frames.

What am I missing?

Thank you!

private void LateUpdate()

    float f = _animator.GetCurrentAnimatorClipInfo(0)(0).clip.length * (_animator.GetCurrentAnimatorStateInfo(0).normalizedTime % 1) * _animator.GetCurrentAnimatorClipInfo(0)(0).clip.frameRate;
    int iCurrentFrameIDInAnimationFile = (int)f;

    bool bIsNewFrame = (iCurrentFrameIDInAnimationFile != _iLastFrameIDInAnimationFile);

    if (!bIsNewFrame)
    else if (bIsNewFrame)
        _iFrameCount += 1;

    _iLastFrameIDInAnimationFile = iCurrentFrameIDInAnimationFile;

    if (_animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !_animator.IsInTransition(0))
        //animation has finished playing
        Debug.Log("Frames: " + _iFrameCount); //this returns 43 frames. Why???

(Unity Engine) Is it possible to make 2d animation bones perfectly straight within the sprite editor

I am experimenting with pixel perfect camera and 2d animation. I noticed in the skinning editor that there is no option to edit bone coordinates. When I make an ik it extended the arm making it noticeable that the bones weren’t perfectly aligned. so im wondering if it is possible to make a bone perfectly vertical/horizontal within the skinning editor.