13 - Powering Down the Character

为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 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(UGameplayStatics::GetPlayerPawn(this, 0)); //获得Character
if (MyCharacter)
{
if (MyCharacter->GetCurrentPower() > 0)
{
MyCharacter->UpdatePower(-DeltaSecondsDecayRate(MyCharacter->GetInitialPower()));
}

}

}

修改场景的GameMode

13 - Powering Down the Character_第1张图片
Paste_Image.png

运行程序 观察Character的CharacterPower 不停衰减

你可能感兴趣的:(13 - Powering Down the Character)