Hi guys!
i want to sctrictly play a idle animation using c++ only …
currently i m doing like this…
char.h
UCLASS()
class TREBUCKET_1_API Atb_enemyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character’s properties
Atb_enemyCharacter();
UAnimSequence* IdleAnim;
USkeleton* meshSkeleton;
UMaterialInterface* mansQmat;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
char.cpp
// Sets default values
Atb_enemyCharacter::Atb_enemyCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;
GetCapsuleComponent()->SetupAttachment(RootComponent);
GetCapsuleComponent()->SetCapsuleHalfHeight(96.f);
GetCapsuleComponent()->SetCapsuleRadius(42.f);
static ConstructorHelpers::FObjectFinder<USkeletalMesh> skMeshAsset(TEXT("/Game/TB_1/Mannequin/Character/Mesh/SK_Mannequin.SK_Mannequin"));
if (skMeshAsset.Succeeded())
{
GetMesh()->SetSkeletalMesh(skMeshAsset.Object);
GetMesh()->SetRelativeLocation(FVector(0.f, 0.f, -97.f));
GetMesh()->SetRelativeRotation(FRotator(0.f, 270.f, 0.f));
GetMesh()->SetVisibility(true, true);
GetMesh()->VisibilityBasedAnimTickOption = EVisibilityBasedAnimTickOption::AlwaysTickPoseAndRefreshBones;
GetMesh()->SetAnimationMode(EAnimationMode::AnimationSingleNode);
}
meshSkeleton = CreateDefaultSubobject<USkeleton>(TEXT("meshskeleton_1"));
static ConstructorHelpers::FObjectFinder<USkeleton> skeletonObj(TEXT("/Game/TB_1/Mannequin/Character/Mesh/UE4_Mannequin_Skeleton.UE4_Mannequin_Skeleton"));
if (skeletonObj.Succeeded())
{
meshSkeleton = skeletonObj.Object;
}
static ConstructorHelpers::FObjectFinder<UAnimSequence> animObj(TEXT("/Game/TB_1/Mannequin/Animations/ThirdPersonIdle.ThirdPersonIdle"));
IdleAnim = animObj.Object;
IdleAnim->SetSkeleton(meshSkeleton);
mansQmat = CreateDefaultSubobject<UMaterial>(TEXT("manQmat_1"));
static ConstructorHelpers::FObjectFinder<UMaterial> skMatAsset(TEXT("/Game/TB_1/Mannequin/Character/Materials/M_UE4Man_Body.M_UE4Man_Body"));
if (skMatAsset.Succeeded())
{
//skMesh->SetMaterial(0, mansQmat);
GetMesh()->SetMaterial(0, mansQmat);
}
}
// Called every frame
void Atb_enemyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//GetMesh()->SetAnimation(IdleAnim);
if (IdleAnim->IsValidToPlay())
{
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("PlayAnimation"));
GetMesh()->SetPlayRate(0.5f);
GetMesh()->PlayAnimation(IdleAnim, true);
}
}