Unreal Engine 4 —— GAS系统学习 (一) 准备工作

 

在学习GAS系统之间需要准备的一些内容。

今天开始进入UE4的GAS系统学习,该系统是角色的技能系统,全称为Gameplay Abilities System。

首先我们需要开启GameplayAbilities和GameplayTagsEditor两个模块。

新建项目,在插件中找到这两个模块,并启用,重启客户端。

 

 

在UE4商城中下载红框资源(免费),添加到工程,并用C++创建一个Character文件。

 

 

打开xxx.Build.cs文件,增加以下两个模块。编译后重启。

 

为Character的C++文件添加模型,属性,动作映射,蒙太奇动画,动画融合,动画通知事件等内容。

 

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "GameFramework/Character.h"
 7 #include "GameFramework/CharacterMovementComponent.h"
 8 #include "Components/CapsuleComponent.h"
 9 #include "AbilitySystemInterface.h"
10 #include "AbilitySystemComponent.h"
11 #include "Abilities/GameplayAbility.h"
12 #include "GameplayAbilities/Public/GameplayAbilitySpec.h"
13 #include "Shinbi.generated.h"
14 
15 UCLASS()
16 class SHIBI_API AShinbi : public ACharacter,public IAbilitySystemInterface
17 {
18     GENERATED_BODY()
19 
20 public:
21     // Sets default values for this character's properties
22     AShinbi();
23 
24 protected:
25     // Called when the game starts or when spawned
26     virtual void BeginPlay() override;
27 
28 public:    
29     // Called every frame
30     virtual void Tick(float DeltaTime) override;
31 
32     // Called to bind functionality to input
33     virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
34 
35     UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="Shinbi | Properties")
36     float LookRate;
37     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shinbi | Properties")
38     float LookUpRate;
39 
40     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shinbi | Properties")
41     float MoveSpeed;
42 
43     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shinbi | Properties")
44     FRotator mouseInput;
45 
46     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shinbi | Properties")
47     FVector keyInput;
48 
49     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shinbi | Properties")
50     class UCharacterMovementComponent* moveComponent;
51 
52 public:
53     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Shinbi | Camera")
54     class USkeletalMeshComponent* shibiSkeletal;
55 
56     UPROPERTY(VisibleAnywhere,BlueprintReadWrite, Category = "Shinbi | Camera")
57     class UCameraComponent* cameras;
58 
59     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Shinbi | Camera")
60     class USpringArmComponent* springArm;
61 
62     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Shinbi | Animation")
63     UAnimMontage* montageAttack;
64 
65     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Shinbi | AbilitiesComponent")
66     class UAbilitySystemComponent* AbilityComp;
67 
68     virtual UAbilitySystemComponent* GetAbilitySystemComponent() const;
69 
70     UFUNCTION(BlueprintCallable, Category = "Shinbi | UGameplayAbilities")
71     void AquireAbility(TSubclassOf<UGameplayAbility> listAblities);
72 
73     void moveForward(float value);
74     void moveLeft(float value);
75     void lookUp(float value);
76     void lookRight(float value);
77     void Jump();
78     void Attack();
79 };

 

1 // Fill out your copyright notice in the Description page of Project Settings.
  2 
  3 
  4 #include "Shinbi.h"
  5 #include "UObject/ConstructorHelpers.h"
  6 #include "Components/SkeletalMeshComponent.h"
  7 #include "Camera\CameraComponent.h"
  8 #include "GameFramework/SpringArmComponent.h"
  9 #include "Components/InputComponent.h"
 10 
 11 // Sets default values
 12 AShinbi::AShinbi()
 13 {
 14      // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
 15     PrimaryActorTick.bCanEverTick = true;
 16     //找到父类中的Skeletal组件加载资源
 17     shibiSkeletal = FindComponentByClass<USkeletalMeshComponent>();
 18 
 19     //shibiSkeletal = GetMesh();
 20     
 21     AbilityComp = CreateDefaultSubobject<UAbilitySystemComponent>("AbilityComp");
 22 
 23 
 24     //创建相机与摇臂
 25     cameras = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
 26     springArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("springArm"));
 27     moveComponent = FindComponentByClass<UCharacterMovementComponent>();
 28     springArm->SetupAttachment(shibiSkeletal);
 29     cameras->SetupAttachment(springArm);
 30 
 31     moveComponent->JumpZVelocity = 840.0f;
 32 
 33     auto shibiSkeletalAsset = ConstructorHelpers::FObjectFinder<USkeletalMesh>(TEXT("SkeletalMesh'/Game/ParagonShinbi/Characters/Heroes/Shinbi/Meshes/Shinbi.Shinbi'"));
 34     
 35 
 36     AutoPossessPlayer = EAutoReceiveInput::Player0;
 37 
 38     //设置模型基本属性
 39     if (shibiSkeletalAsset.Succeeded()) {
 40         shibiSkeletal->SetSkeletalMesh(shibiSkeletalAsset.Object);
 41         shibiSkeletal->SetRelativeLocation(FVector(0, 0, -90));
 42         shibiSkeletal->SetRelativeRotation(FRotator(0, -90, 0));
 43         shibiSkeletal->SetCollisionResponseToAllChannels(ECR_Block);
 44         shibiSkeletal->SetCollisionObjectType(ECC_Pawn);
 45         springArm->CameraLagSpeed = 100.0f;
 46         springArm->TargetArmLength = 300.0f;
 47         springArm->bEnableCameraLag = true;
 48         springArm->SetRelativeRotation(FRotator(-10, 90, 0));
 49         springArm->SetRelativeLocation(FVector(0, 0, 150));
 50 
 51     }
 52     MoveSpeed = 20.0f;
 53     LookRate = 5.0f;
 54     LookUpRate = 5.0f;
 55 }
 56 
 57 // Called when the game starts or when spawned
 58 void AShinbi::BeginPlay()
 59 {
 60     Super::BeginPlay();
 61     UClass* anim = LoadClass<UAnimInstance>(NULL,TEXT("AnimBlueprint'/Game/BP/Anim_BP_Shibi.Anim_BP_Shibi_C'"));
 62     shibiSkeletal->SetAnimInstanceClass(anim);
 63 
 64     //加载蒙太奇攻击动画资源
 65     montageAttack = LoadObject<UAnimMontage>(NULL, TEXT("AnimMontage'/Game/BP/Anim_Attack.Anim_Attack'"));
 66     
 67 }
 68 
 69 // Called every frame
 70 void AShinbi::Tick(float DeltaTime)
 71 {
 72     Super::Tick(DeltaTime);
 73     //设置人物移动
 74 
 75 
 76     //设置人物UP视角
 77 
 78     FRotator springArmRotator = springArm->GetRelativeRotation();
 79     mouseInput.Pitch *= LookUpRate;
 80     springArmRotator.Pitch += mouseInput.Pitch;
 81     springArmRotator.Pitch = FMath::Clamp(springArmRotator.Pitch, -30.0f, 30.0f);
 82     springArm->SetRelativeRotation(springArmRotator);
 83 
 84     //设置人物YAW视角
 85     FRotator cameraRotation = cameras->GetRelativeRotation();
 86     mouseInput.Yaw *= LookRate;
 87     cameraRotation.Yaw += mouseInput.Yaw;
 88     cameraRotation.Yaw = FMath::Clamp(cameraRotation.Yaw, -180.0f, 180.0f);
 89     shibiSkeletal->AddRelativeRotation(cameraRotation);
 90 
 91 
 92     //设置人物跟随视角移动,改变移动方向。
 93 
 94     FVector left = shibiSkeletal->GetForwardVector();
 95     FVector forward = shibiSkeletal->GetRightVector();
 96 
 97     if (keyInput.Y == 20) {
 98         keyInput= forward * MoveSpeed;
 99     }
100     else if (keyInput.Y == -20) {
101         keyInput = -(forward * MoveSpeed);
102     }
103 
104     if (keyInput.X == 20) {
105         keyInput = left * MoveSpeed;
106     }
107     else if(keyInput.X == -20) {
108         keyInput = -(left * MoveSpeed);
109     }
110 
111     AddActorLocalOffset(keyInput);
112 
113 }
114 
115 
116 // Called to bind functionality to input
117 void AShinbi::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
118 {
119     Super::SetupPlayerInputComponent(PlayerInputComponent);
120 
121     PlayerInputComponent->BindAxis("moveForward", this, &AShinbi::moveForward);
122     PlayerInputComponent->BindAxis("moveLeft", this, &AShinbi::moveLeft);
123     PlayerInputComponent->BindAxis("lookUp", this, &AShinbi::lookUp);
124     PlayerInputComponent->BindAxis("lookRight", this, &AShinbi::lookRight);
125     PlayerInputComponent->BindAction("Jump",IE_Pressed, this, &AShinbi::Jump);
126     PlayerInputComponent->BindAction("Attack",IE_Pressed, this, &AShinbi::Attack);
127 
128 }
129 
130 void AShinbi::moveLeft(float value)
131 {
132     //DA控制的时候
133     keyInput.X = FMath::Clamp(value,-1.0f,1.0f) * MoveSpeed;
134 }
135 
136 UAbilitySystemComponent* AShinbi::GetAbilitySystemComponent() const
137 {
138     return AbilityComp;
139 }
140 
141 //调用这个方法,给主角增加一个游戏技能,选择游戏技能是从UGameplayAbility的子类中选取
142 void AShinbi::AquireAbility(TSubclassOf<UGameplayAbility> listAblities)
143 {
144     if (AbilityComp) {
145         if (HasAuthority() && listAblities) {
146             //为主角添加技能,FGameplayAbilitySpec是从菜单中选取的具体的技能,并将技能的各项属性打包到FGameplayAbilitySpec结构体中
147             //通过AbilityComp添加技能。而AbilityComp组件是属于主角的,所以主角可以拥有该技能。
148             AbilityComp->GiveAbility(FGameplayAbilitySpec(listAblities,1,0));
149         }
150         //初始化主角技能,填入技能的拥有者和发起者。
151         AbilityComp->InitAbilityActorInfo(this, this);
152     }
153 }
154 
155 void AShinbi::moveForward(float value)
156 {
157     //WS控制的时候
158     keyInput.Y = FMath::Clamp(value, -1.0f, 1.0f) * MoveSpeed;
159 
160 }
161 
162 void AShinbi::lookUp(float value)
163 {
164     mouseInput.Pitch = value;
165     mouseInput.Yaw = 0;
166     mouseInput.Roll = 0;
167     
168 }
169 
170 void AShinbi::lookRight(float value)
171 {
172     mouseInput.Yaw = value;
173     mouseInput.Pitch = 0;
174     mouseInput.Roll = 0;
175 
176 }
177 
178 void AShinbi::Jump()
179 {
180     Super::Jump();
181 }
182 
183 void AShinbi::Attack()
184 {
185     //播放蒙太奇动画
186     PlayAnimMontage(montageAttack);
187 }

动画混合空间

 

 

 

动画蓝图,状态机等设置

 

 

 

攻击动画蒙太奇及通知事件

 

 

 

 

一切准备就绪。

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值