Search code examples
c++animationunreal-engine4

Playing an Animation on the SkeletalMesh of a Pawn


In Unreal 4, I'm trying to play an animation for a Gun from a Pawn we controlled throught Virtual Reality. I tried making the same animation on a Character that has a skeletalmesh and everything went fine. Now I'm in a Pawn with the same skeletalmesh and the animation isn't playing.

SlickBoyMotionControllerPawn.cpp

// Function where I trigger the animation
void ASlickBoyMotionControllerPawn::Fire(ASlickBoyMotionController *Current)
{
        // Rest of the code that handle the fire of the gun

        // Current is the gun that fires (motion control)
        USkeletalMeshComponent* currentMesh = Current->GetHandMesh();
        if (currentMesh) {
            currentMesh->PlayAnimation(knockBack, false);
            currentMesh->PlayAnimation(trigger, false);
        }
}

SlickBoyMotionControllerPawn.h

UCLASS()
class SLICKBOY_API ASlickBoyMotionControllerPawn : public APawn, public IAttackable
{
    GENERATED_BODY()
public:
    // other stuff

    // Gun Anim
    UAnimSequence *knockBack;
    UAnimSequence *trigger;

    UFUNCTION()
    void Fire(class ASlickBoyMotionController* Hand);
}

Note that everything compiles and I don't have any error while executing the code


Solution

  • The knockback sequence will not play, because you are immediately overwriting the animation with trigger.

    currentMesh->PlayAnimation(knockBack, false);
    currentMesh->PlayAnimation(trigger, false);
    

    PlayAnimation is a non-blocking call, the code will not wait until knockback has finished playing before starting trigger.

    If you want to play two animations consecutively, use an Animation Composite