1 下载模型并导入
从mixamo上Characters页面下载一个人物骨骼模型,然后去Animations页面下载一个Idle动画,一个Running动画(勾上In Place,让动画在原地跑即可),将下载好的模型拖入UE4中。
2 创建人物
基于Character类新建一个C++类,添加能跟随人物移动的相机组件。
class UCameraComponent;
class USpringArmComponent;
class USceneComponent;
UCLASS()
class TESTMODEL_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
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;
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
private:
UPROPERTY(VisibleAnywhere, Category = "Camera")
USpringArmComponent* SpringArmCameraComp;
UPROPERTY(VisibleAnywhere, Category = "Camera")
UCameraComponent* CameraComp;
};
AMyCharacter::AMyCharacter()
{
// 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;
static ConstructorHelpers::FObjectFinder<USkeletalMesh> mesh(TEXT("/Game/Assets/aj"));
GetMesh()->SetSkeletalMesh(mesh.Object);
SpringArmCameraComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
SpringArmCameraComp->SetupAttachment(RootComponent);
SpringArmCameraComp->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-30.0f, 0.0f, 0.0f));
SpringArmCameraComp->TargetArmLength = 400.0f;
SpringArmCameraComp->bEnableCameraLag = true;
SpringArmCameraComp->CameraLagSpeed = 3.0f;
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComp->SetupAttachment(SpringArmCameraComp);
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
}
void AMyCharacter::MoveForward(float AxisValue)
{
AddMovementInput(FVector::ForwardVector, AxisValue);
}
void AMyCharacter::MoveRight(float AxisValue)
{
AddMovementInput(FVector::RightVector, AxisValue);
}
3 设置人物及相机属性
为刚创建的C++人物类添加一个蓝图类,调整Mesh组件下人物的方向(正方向对着蓝色箭头);点击CharacterMovement组件,在Details面板勾上Orient Rotation to Movement。
点开根组件,将Use Controller Rotation Yaw的勾去掉,这样人物模型就能跟着移动的方向旋转,而不是只会对着正前方。
但是这样相机也会跟着旋转,所以还需点开弹簧臂组件SpringArmCamera,在Camera Settings里勾上Use Pawn Control Rotation
到此,在项目设置里添加好对应的WSAD轴映射之后,就可以控制人物前后左右移动了。但是人物模型还是处于十字架状态,所以还需要添加动画
4 创建动画
首先创建一个Blend Space 1D动画,点开之后将轴的名字改为Speed,表示根据移动速度来播放动画,将下载好的Idle动画和Running动画分别拖到Speed为0和最大值所对应的位置
创建一个动画蓝图Animation Blueprint,点开后添加一个新的状态机,此处命名为Locomotion,点开之后将上面创建的混合动画拖进来,连到Entry。由于是根据速度来播放动画的,所以需要新建一个float类型的变量来设置和获取人物的速度,将速度赋值给混合动画
速度的值当然得去实时获取。点开事件图表,获取人物蓝图类,每一帧实时将人物速度赋值给变量Speed
将动画蓝图赋值到人物蓝图类中
至此就能根据人物速度正常播放静止和跑步的动画了。