UE5 C++ Gas开发 学习记录(一)

一个新坑,在TPS的空余时间学习

创建了自己,敌人的BaseCharacter和子类,创建了Gamemode,创建了Controller

AuraCharacterBase.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/Character.h" #include "AuraCharacterBase.generated.h" UCLASS(Abstract) class MYGAS_API AAuraCharacterBase : public ACharacter { GENERATED_BODY() public: // Sets default values for this character's properties AAuraCharacterBase(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; //TObjectPtr是一个模板类,用来指向UObject,并且在被销毁的时候自动设置为nullptr UPROPERTY(EditAnywhere,Category="Combat") TObjectPtr<USkeletalMeshComponent> Weapon; };

// Fill out your copyright notice in the Description page of Project Settings. #include "Character/AuraCharacterBase.h" #include "Components/SkeletalMeshComponent.h" // Sets default values AAuraCharacterBase::AAuraCharacterBase() { // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = false; //创建Weapon Weapon = CreateDefaultSubobject<USkeletalMeshComponent>("Weapon"); //初始化所需的Attach Parent和SocketName,以便在组件注册时附加 Weapon->SetupAttachment(GetMesh(),FName("WeaponHandSocket")); //设置Weapon的碰撞为NoCollision Weapon->SetCollisionEnabled(ECollisionEnabled::NoCollision); } // Called when the game starts or when spawned void AAuraCharacterBase::BeginPlay() { Super::BeginPlay(); }

AuraCharacter.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Camera/CameraComponent.h" #include "Character/AuraCharacterBase.h" #include "GameFramework/SpringArmComponent.h" #include "AuraCharacter.generated.h" /** * */ UCLASS() class MYGAS_API AAuraCharacter : public AAuraCharacterBase { AAuraCharacter(); GENERATED_BODY() UPROPERTY(EditAnywhere,Category="My") UCameraComponent* FollowCamera; UPROPERTY(EditAnywhere,Category="My") USpringArmComponent* CameraBoom; public: /** Returns CameraBoom subobject **/ FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; } /** Returns FollowCamera subobject **/ FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; } };

// Fill out your copyright notice in the Description page of Project Settings. #include "Character/AuraCharacter.h" #include "GameFramework/CharacterMovementComponent.h" AAuraCharacter::AAuraCharacter() { //初始化弹簧臂,并且绑定在Root上 CameraBoom = CreateDefaultSubobject<USpringArmComponent>("CameraBoom"); CameraBoom ->SetupAttachment(RootComponent); CameraBoom->TargetArmLength = 400.f; CameraBoom->bUsePawnControlRotation = true; //初始化相机,并且安装到弹簧臂上 FollowCamera = CreateDefaultSubobject<UCameraComponent>("FollowCamera"); FollowCamera->SetupAttachment(CameraBoom,USpringArmComponent::SocketName); FollowCamera->bUsePawnControlRotation = false; //初始化角色移动 //控制角色是否朝着速度的方向进行旋转 GetCharacterMovement()->bOrientRotationToMovement= true; //控制旋转的速度 GetCharacterMovement()->RotationRate = FRotator(0.f,400.f,0.f); //控制移动是否在平面上 GetCharacterMovement()->bConstrainToPlane = true; //当在平面为true的时候,是否强制与平面对齐 GetCharacterMovement()->bSnapToPlaneAtStart = true; bUseControllerRotationPitch = false; bUseControllerRotationRoll = false; bUseControllerRotationYaw = false; }

AuraEnemy.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Character/AuraCharacterBase.h" #include "Interaction/EnemyInterface.h" #include "AuraEnemy.generated.h" /** * */ UCLASS() class MYGAS_API AAuraEnemy : public AAuraCharacterBase , public IEnemyInterface { GENERATED_BODY() public: virtual void HighlightActor() override; virtual void UnHighlightActor() override; };

// Fill out your copyright notice in the Description page of Project Settings. #include "Character/AuraEnemy.h" void AAuraEnemy::HighlightActor() { } void AAuraEnemy::UnHighlightActor() { }

EnemyInterface接口

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "UObject/Interface.h" #include "EnemyInterface.generated.h" // This class does not need to be modified. UINTERFACE(MinimalAPI) class UEnemyInterface : public UInterface { GENERATED_BODY() }; /** * */ class MYGAS_API IEnemyInterface { GENERATED_BODY() // Add interface functions to this class. This is the class that will be inherited to implement this interface. public: virtual void HighlightActor() = 0; virtual void UnHighlightActor() = 0; };

AuraPlayerController.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "../../../../../../../../Program Files/Epic Games/UE_5.1/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/InputMappingContext.h" #include "GameFramework/PlayerController.h" #include "AuraPlayerController.generated.h" //声明一个UInputMappingContext类.然后在下面直接调用这个类 class UInputMappingContext; class UInputAction; struct FInputActionValue; /** * */ UCLASS() class MYGAS_API AAuraPlayerController : public APlayerController { GENERATED_BODY() public: AAuraPlayerController(); protected: virtual void BeginPlay() override; virtual void SetupInputComponent() override; private: UPROPERTY(EditAnywhere,Category="Input") TObjectPtr<UInputMappingContext> AuraContext; UPROPERTY(EditAnywhere,Category="Input") TObjectPtr<UInputAction> MoveAction; void Move(const struct FInputActionValue& InputActionValue); };

// Fill out your copyright notice in the Description page of Project Settings. #include "PlayerController/AuraPlayerController.h" #include "../../../../../../../../Program Files/Epic Games/UE_5.1/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/EnhancedInputComponent.h" #include "../../../../../../../../Program Files/Epic Games/UE_5.1/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/EnhancedInputSubsystems.h" #include "Engine/LocalPlayer.h" #include "GameFramework/Pawn.h" AAuraPlayerController::AAuraPlayerController() { bReplicates = true; } void AAuraPlayerController::BeginPlay() { Super::BeginPlay(); //检查AuraContext是否有效 check(AuraContext); UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()); check(Subsystem); //Add a control mapping context. 第一个参数AuraContext是映射集合,第二个参数是映射的优先级 Subsystem->AddMappingContext(AuraContext,0); //设置鼠标光标行为和输入模式 //是否显示鼠标 bShowMouseCursor=true; //光标默认样式 DefaultMouseCursor = EMouseCursor::Default; //设置游戏和UI的输入模式 FInputModeGameAndUI InputModeData; //鼠标可以移动到视口外 InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock); //不隐藏鼠标 InputModeData.SetHideCursorDuringCapture(false); SetInputMode(InputModeData); } void AAuraPlayerController::SetupInputComponent() { Super::SetupInputComponent(); UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent); //在启动的时候绑定MoveAction组件,并且把输入的参数传递给Move函数 EnhancedInputComponent->BindAction(MoveAction,ETriggerEvent::Triggered,this,&AAuraPlayerController::Move); } void AAuraPlayerController::Move(const FInputActionValue& InputActionValue) { // const FVector2d InputAxisVector = InputActionValue.Get<FVector2D>(); const FRotator Rotation = GetControlRotation(); const FRotator YawRotation(0.f,Rotation.Yaw,0.f); const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); // if(APawn* ControlledPawn = GetPawn<APawn>()) { ControlledPawn->AddMovementInput(ForwardDirection,InputAxisVector.Y); ControlledPawn->AddMovementInput(RightDirection,InputAxisVector.X); } }

AuraGameModeBase.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "GameFramework/GameModeBase.h" #include "AuraGameModeBase.generated.h" /** * */ UCLASS() class MYGAS_API AAuraGameModeBase : public AGameModeBase { GENERATED_BODY() };

创建角色,继承自Character

怪物也同理,但是新制作了怪物的动画蓝图模板

怪物的动画蓝图继承自怪物模板

制作了输入

制作了PlayController

相关推荐
Alice-YUE1 分钟前
【css学习笔记8】html5css3新特性
css·笔记·学习
愚润求学42 分钟前
【贪心算法】day8
c++·算法·leetcode·贪心算法
平生不喜凡桃李1 小时前
C++ 异常
android·java·c++
有谁看见我的剑了?1 小时前
k8s-Sidecar容器学习
学习·容器·kubernetes
小伟童鞋1 小时前
c++中导出函数调用约定为__stdcall类型函数并指定导出函数名称
开发语言·c++
维C泡泡1 小时前
C++初认、命名规则、输入输出、函数重载、引用+coust引用
开发语言·c++
沐墨专攻技术1 小时前
二、网页的“化妆师”:从零学习 CSS
css·笔记·学习
青草地溪水旁1 小时前
设计模式(C++)详解——建造者模式(2)
c++·设计模式·建造者模式
g_i_a_o_giao2 小时前
Android8 binder源码学习分析笔记(四)——ServiceManager启动
笔记·学习·binder
GilgameshJSS2 小时前
【学习K230-例程23】GT6700-音频FFT柱状图
python·学习·音视频