Create Animation in C# for Capsule-Characterbody3D

Godot Version

Godot.Net 4.2.1

Question

Hi,

i try to create a animation in C# and if i press the button “C” to play the animation.

What i have try?

  • Create a AnimationPlayer
  • Create AnimationLibrary
  • Create a Animation
  • Add a Track zu the Animation (TrackType.Scale3D)
  • Add the Track zu Animation
  • Add the Animation to the AnimationLibrary
  • And add the AnimationLibrary to the AnimationPlayer

But if i press the C-Button and the C#-Code starts the animation with:
AnimationPlayer.Play(“CapsuleCrouch”);

I receive the error message, animation “CapsuleCrouch” not found.

This is my C#-Code:

            // Add animation for crouching
            AnimationPlayer = new AnimationPlayer();
            AnimationLibrary AniLib = new AnimationLibrary();
            Animation a = new Animation();

            a.AddTrack(Animation.TrackType.Scale3D);
            AniLib.AddAnimation("CapsuleCrouch", a);
            AnimationPlayer.Name = "CapsuleCrouch";
            AnimationPlayer.AddAnimationLibrary("CapsuleCrouch", AniLib);

            AddChild(AnimationPlayer);

I have try to found a solution with google, but for C# ?
I hope anyone can help me. please :slight_smile:

Best regards
Andy

It’s not being found because you are creating an AnimationLibrary with the name CapsuleCrouchand adding that animation to it. It should be AnimationPlayer.Play('CapsuleCrouch/CapsuleCrouch')

If you want to add the animation to the Global AnimationLibrary of the AnimationPlayer then get the library with AnimationMixer.get_animation_library() (the documentation tells you how to do it) and add the animation to that library.

1 Like

I was sooo close… Thank you :slight_smile: