UE4 从无到有纯 C++ & Slate 开发沙盒游戏(十九) 动作蓝图与玩家移动
作动作蓝图之前,先新建一个碰撞
新建一个碰撞类型,在 编辑>项目设置,引擎>Collision>Object Channels 下新建一个碰撞类型
输入碰撞对象名,待在地面上默认响应选择 Block ,新建一个碰撞对象
Preset 选择新建,以下方式设置,这一步是去设置这个碰撞的一些碰撞属性,设置完后点击接受
目前只有这一个,以后会有很多
然后前往 D:\UE4 Project\UE26.2\CourseProject\SlAiCourse\Source\SlAiCourse\Private\Player\SlAiPlayerCharacter.cpp 下设置他的碰撞体
1 // Fill out your copyright notice in the Description page of Project Settings. 2 3 …… 4 #include "Components/CapsuleComponent.h" 5 6 // Sets default values 7 ASlAiPlayerCharacter::ASlAiPlayerCharacter() 8 { 9 10 PrimaryActorTick.bCanEverTick = true; 11 12 //开始设置人物磁撞体的属性为 PlayerProfile,下面的骨骼模型的碰撞就都可以设置为无碰撞 13 GetCapsuleComponent()->SetCollisionProfileName(FName("PlayerProfile")); 14 15 ……
接下来去设置角色的动作蓝图
之前已经创建了动作蓝图对应的C++文件,先去改一下这些文件
SlAiPlayerAnim 是动作蓝图的基类
SlAiThirdPlayerAnim 是继承于 SlAiPlayerAnim 的第三人称基类
在 SlAiPlayerAnim 下添加一些参数
D:\UE4 Project\UE26.2\CourseProject\SlAiCourse\Source\SlAiCourse\Public\Player\SlAiPlayerAnim.h
1 // Fill out your copyright notice in the Description page of Project Settings. 2 3 #pragma once 4 5 #include "CoreMinimal.h" 6 #include "Animation/AnimInstance.h" 7 #include "SlAiPlayerAnim.generated.h" 8 9 /** 10 * 11 */ 12 UCLASS() 13 class SLAICOURSE_API USlAiPlayerAnim : public UAnimInstance 14 { 15 GENERATED_BODY() 16 17 public: 18 USlAiPlayerAnim(); 19 20 public: 21 //EditAnywhere 允许在蓝图里设置 22 //BlueprintReadOnly 在蓝图中只允许读 23 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = PlayAnim) 24 float Speed; //速度 25 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = PlayAnim) 26 FRotator SpineRotator; //上半身旋转 27 };
D:\UE4 Project\UE26.2\CourseProject\SlAiCourse\Source\SlAiCourse\Private\Player\SlAiPlayerAnim.cpp
1 // Fill out your copyright notice in the Description page of Project Settings. 2 3 4 #include "Player/SlAiPlayerAnim.h" 5 6 USlAiPlayerAnim::USlAiPlayerAnim() 7 { 8 Speed = 0.f; 9 SpineRotator = FRotator(0.f, 0.f, 0.f); 10 }
先做第三人称动作蓝图,SlAiThirdPlayerAnim 是继承于 SlAiPlayerAnim 的,所以他也继承了SlAiPlayerAnim 中的两个变量,第三人称需要速度,上半身旋转,和一个是否跳跃
D:\UE4 Project\UE26.2\CourseProject\SlAiCourse\Source\SlAiCourse\Public\Player\SlAiThirdPlayerAnim.h
1 // Fill out your copyright notice in the Description page of Project Settings. 2 3 #pragma once 4 5 #include "CoreMinimal.h" 6 #include "Player/SlAiPlayerAnim.h" 7 #include "SlAiThirdPlayerAnim.generated.h" 8 9 /** 10 * 11 */ 12 UCLASS() 13 class SLAICOURSE_API USlAiThirdPlayerAnim : public USlAiPlayerAnim 14 { 15 GENERATED_BODY() 16 public: 17 USlAiThirdPlayerAnim(); 18 19 public: 20 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = PlayAnim) 21 bool IsInAir; //是否在空中 22 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = PlayAnim) 23 float Direction; //方向 24 };
D:\UE4 Project\UE26.2\CourseProject\SlAiCourse\Source\SlAiCourse\Private\Player\SlAiThirdPlayerAnim.cpp
1 // Fill out your copyright notice in the Description page of Project Settings. 2 3 4 #include "Player/SlAiThirdPlayerAnim.h" 5 6 USlAiThirdPlayerAnim::USlAiThirdPlayerAnim() 7 { 8 //初始化是否在空中 9 IsInAir = false; 10 //初始化方向 11 Direction = 0.f; 12 }
接下来在UE4中,对动作蓝图进行编辑
现在 BluePrint 文件夹下新建一个文件 Player,并在 Player 文件夹下创建两个动作蓝图
右侧的配置就是
接下来绑定动作,新建一个动作状态机,命名 LocalMotion