How to play animations by code?

Hello ,

Here is a small code snippet. You can try to create the AnimSequence in your character and play it.

Creating of animation:

YourCharacter.h

UAnimSequence *Anim;

class YourCharacter : public ACharacter
{
	...
	
	UAnimSequence *Anim;
	
	...
}

YourCharacter.cpp

...

AMyProject3Character::AMyProject3Character()
{
	...
	
	static ConstructorHelpers::FObjectFinder<UAnimSequence> anim(TEXT("AnimSequence'/Game/Mannequin/Animations/ThirdPersonJump_Start.ThirdPersonJump_Start'"));
	Anim = anim.Object;
	
	...
}

...

Playing animation:

...

bool bLoop = false;
GetMesh()->PlayAnimation(Anim, bLoop);

...

AnimSequence’/Game/Mannequin/Animations/ThirdPersonJump_Start.ThirdPersonJump_Start’ - reference of your AnimSequence from engine.

Hope this helps.

3 Likes