UE4 C++ Character的动画(C++类)
Character类C++的蓝图类
再建一个继承AnimInstance的C++类
继承AnimInstance的C++类的代码:
#pragma once
#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "MainPlayerAnimInstance.generated.h"
UCLASS()
class GETSTARTED_API UMainPlayerAnimInstance : public UAnimInstance
{
GENERATED_BODY()
public :
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Animation Properties")
float Speed;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Animation Properties")
bool bIsInAir;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Animation Properties")
class AMyinPlayer* PlayerRef;
public:
virtual void NativeInitializeAnimation() override;
UFUNCTION(BlueprintCallable,Category="Animation Properties")
void UpdateAnimationProperties();
};
#include "Characters/Player/MainPlayerAnimInstance.h"
#include "Characters/Player/MyinPlayer.h"
#include "GameFramework/CharacterMovementComponent.h"
void UMainPlayerAnimInstance::NativeInitializeAnimation()
{
PlayerRef = Cast<AMyinPlayer>(TryGetPawnOwner());
}
void UMainPlayerAnimInstance::UpdateAnimationProperties()
{
if (PlayerRef == nullptr) {
PlayerRef = Cast<AMyinPlayer>(TryGetPawnOwner());
}
if (PlayerRef) {
FVector SpeedFvector = PlayerRef->GetVelocity();
FVector PlanarSpeed = FVector(SpeedFvector.X, SpeedFvector.Y, 0.0f);
Speed = PlanarSpeed.Size();
bIsInAir = PlayerRef->GetMovementComponent()->IsFalling();
}
}
再建一个继承这个C++的蓝图动画类:
再写状态机
|