为Character添加能量衰减功能
打开BatteryCollectorGameMode.h
添加变量DecayRate 表示衰减速度
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
pragma once
include "GameFramework/GameModeBase.h"
include "BatteryCollectorGameMode.generated.h"
UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
ABatteryCollectorGameMode();
virtual void Tick(float DeltaSeconds) override; //添加tick 函数 每帧执行衰减
protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
float DecayRate;
};
编辑BatteryCollectorGameMode.cpp
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
include "BatteryCollector.h"
include "BatteryCollectorGameMode.h"
include "BatteryCollectorCharacter.h"
include "Kismet/GameplayStatics.h"
ABatteryCollectorGameMode::ABatteryCollectorGameMode()
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
if (PlayerPawnBPClass.Class != NULL)
{
DefaultPawnClass = PlayerPawnBPClass.Class;
}
DecayRate = 0.01f; //初始化速度 每秒掉1%
PrimaryActorTick.bCanEverTick = true; //开启tick
}
void ABatteryCollectorGameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this, 0)); //获得Character
if (MyCharacter)
{
if (MyCharacter->GetCurrentPower() > 0)
{
MyCharacter->UpdatePower(-DeltaSecondsDecayRate(MyCharacter->GetInitialPower()));
}
}
}
修改场景的GameMode
运行程序 观察Character的CharacterPower 不停衰减