一个新坑,在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