unreal-c++教程-第五章:创建你的角色
ACharacter1.ACharacter常见操作
1.1 创建ACharacter的子类BaseRole1.2 添加BaseRole的网格模型
1.2.1 修改BaseRole的Mesh的局部位置和旋转
1.3 添加BaseRole的动画
1.3.1 创建AnimBlueprint1.3.2 在c++中指定角色的动画类
1.ACharacter常见操作
unreal里面,为了更方便游戏逻辑的构建,单独将角色行为抽象成ACharacter,里面定义了角色的
动画,网格等基本行为,基本可以满足绝大部分游戏的需求,这是一件非常棒的事情!1.1 创建ACharacter的子类BaseRole
BaseRole.h
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Character.h"#include"BaseRole.generated.h"UCLASS()
class UNREALLABX_API ABaseRole : public ACharacter
{GENERATED_BODY()
public:// Sets default values for this character's propertiesABaseRole();
protected:// Called when the game starts or when spawned
virtual voidBeginPlay() override;
public:// Called every frame
virtual voidTick(float DeltaTime) override;// Called to bind functionality to input
virtual voidSetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;};BaseRole.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"BaseRole.h"// Sets default values
ABaseRole::ABaseRole(){// 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;}// Called when the game starts or when spawnedvoid ABaseRole::BeginPlay(){
Super::BeginPlay();}// Called every framevoid ABaseRole::Tick(float DeltaTime){
Super::Tick(DeltaTime);}// Called to bind functionality to inputvoid ABaseRole::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){
Super::SetupPlayerInputComponent(PlayerInputComponent);}1.2 添加BaseRole的网格模型
可以前往mixamo下载你喜欢的模型还有动画因为Unreal 的CDO的特性,所以,我们一般在构造里面实现整个角色内涵的创建
ABaseRole::ABaseRole(){// 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;auto val = this->GetMesh();auto asset = ConstructorHelpers::FObjectFinder<USkeletalMesh>(TEXT("SkeletalMesh'/Game/Demos/Mesh/BaseRole/BaseRole.BaseRole'"));
val->SetSkeletalMesh(asset.Object);}
这是我们加载的资源:
当然,你可以快捷的通过:
生成对应的引用字符串
1.2.1 修改BaseRole的Mesh的局部位置和旋转
我们应该让人物模型的位置往下偏移
基本操作是修改USkeletalMeshComponent的局部位置
比较奇葩的是,它并不会热更到当前的静态场景里面,我不清楚这是Bug还是,反正…
当然,你可以通过以下方式修复上述问题:
我们现在要讨论的另外一个问题,就是碰撞体的正前方
应该要与人物的SkeletalMeshComponent的正前方相同
就像默认角色的这样:
所以,我们可以通过以下代码来实现
1.3 添加BaseRole的动画
1.3.1 创建AnimBlueprint
对应的资源:
1.3.2 在c++中指定角色的动画类
关键代码:
ABaseRole::ABaseRole(){// 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;auto mesh = this->GetMesh();auto asset = ConstructorHelpers::FObjectFinder<USkeletalMesh>(TEXT("SkeletalMesh'/Game/Demos/Mesh/BaseRole/BaseRole.BaseRole'"));
mesh->SetSkeletalMesh(asset.Object);
mesh->SetRelativeLocation(FVector(0,0,-80));
mesh->SetRelativeRotation(FRotator(0,-90,0));
ConstructorHelpers::FClassFinder<UAnimInstance>meshAnima(TEXT("/Game/Demos/Animator/AnimBP/BaseRoleAnimBP"));auto animClass = meshAnima.Class;
mesh->SetAnimClass(animClass);}当我们再度编译的时候,动画就播放了:
下一章,我们将联合第四章的PlayerController 实现 角色的控制
页:
[1]