找回密码
 立即注册
查看: 215|回复: 0

unreal-c++教程-第五章:创建你的角色

[复制链接]
发表于 2023-4-8 18:37 | 显示全部楼层 |阅读模式
ACharacter

    1.ACharacter常见操作
      1.1 创建ACharacter的子类BaseRole1.2 添加BaseRole的网格模型
        1.2.1 修改BaseRole的Mesh的局部位置和旋转
      1.3 添加BaseRole的动画
        1.3.1 创建AnimBlueprint1.3.2 在c++中指定角色的动画类




1.ACharacter常见操作
  1. unreal里面,为了更方便游戏逻辑的构建,单独将角色行为抽象成ACharacter,里面定义了角色的
  2. 动画,网格等基本行为,基本可以满足绝大部分游戏的需求,这是一件非常棒的事情!
复制代码
1.1 创建ACharacter的子类BaseRole

BaseRole.h
  1. // 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()
  2. class UNREALLABX_API ABaseRole : public ACharacter
  3. {GENERATED_BODY()
  4. public:// Sets default values for this character's propertiesABaseRole();
  5. protected:// Called when the game starts or when spawned
  6.         virtual voidBeginPlay() override;
  7. public:// Called every frame
  8.         virtual voidTick(float DeltaTime) override;// Called to bind functionality to input
  9.         virtual voidSetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;};
复制代码
BaseRole.cpp
  1. // Fill out your copyright notice in the Description page of Project Settings.#include"BaseRole.h"// Sets default values
  2. ABaseRole::ABaseRole(){// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  3.         PrimaryActorTick.bCanEverTick = true;}// Called when the game starts or when spawnedvoid ABaseRole::BeginPlay(){
  4.         Super::BeginPlay();}// Called every framevoid ABaseRole::Tick(float DeltaTime){
  5.         Super::Tick(DeltaTime);}// Called to bind functionality to inputvoid ABaseRole::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){
  6.         Super::SetupPlayerInputComponent(PlayerInputComponent);}
复制代码
1.2 添加BaseRole的网格模型
  1. 可以前往mixamo下载你喜欢的模型还有动画
复制代码
因为Unreal 的CDO的特性,所以,我们一般在构造里面实现整个角色内涵的创建
  1. ABaseRole::ABaseRole(){// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  2.         PrimaryActorTick.bCanEverTick = true;auto val = this->GetMesh();auto asset = ConstructorHelpers::FObjectFinder<USkeletalMesh>(TEXT("SkeletalMesh'/Game/Demos/Mesh/BaseRole/BaseRole.BaseRole'"));
  3.         val->SetSkeletalMesh(asset.Object);}
复制代码

这是我们加载的资源:


当然,你可以快捷的通过:


生成对应的引用字符串
1.2.1 修改BaseRole的Mesh的局部位置和旋转


我们应该让人物模型的位置往下偏移
基本操作是修改USkeletalMeshComponent的局部位置

比较奇葩的是,它并不会热更到当前的静态场景里面,我不清楚这是Bug还是,反正…

当然,你可以通过以下方式修复上述问题:


我们现在要讨论的另外一个问题,就是碰撞体的正前方


应该要与人物的SkeletalMeshComponent的正前方相同
就像默认角色的这样:


所以,我们可以通过以下代码来实现



1.3 添加BaseRole的动画

1.3.1 创建AnimBlueprint


对应的资源:



1.3.2 在c++中指定角色的动画类

关键代码:
  1. ABaseRole::ABaseRole(){// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  2.         PrimaryActorTick.bCanEverTick = true;auto mesh = this->GetMesh();auto asset = ConstructorHelpers::FObjectFinder<USkeletalMesh>(TEXT("SkeletalMesh'/Game/Demos/Mesh/BaseRole/BaseRole.BaseRole'"));
  3.         mesh->SetSkeletalMesh(asset.Object);
  4.         mesh->SetRelativeLocation(FVector(0,0,-80));
  5.         mesh->SetRelativeRotation(FRotator(0,-90,0));
  6.         ConstructorHelpers::FClassFinder<UAnimInstance>meshAnima(TEXT("/Game/Demos/Animator/AnimBP/BaseRoleAnimBP"));auto animClass = meshAnima.Class;
  7.         mesh->SetAnimClass(animClass);}
复制代码
当我们再度编译的时候,动画就播放了:


下一章,我们将联合第四章的PlayerController 实现 角色的控制

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-15 12:22 , Processed in 0.092686 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表