Skip to content

Instantly share code, notes, and snippets.

@Arixsus
Last active December 19, 2019 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arixsus/39488314ba800ab6626a0fbc7c47dcbb to your computer and use it in GitHub Desktop.
Save Arixsus/39488314ba800ab6626a0fbc7c47dcbb to your computer and use it in GitHub Desktop.
Falling Damage through Gameplay Ability System
#include "PlayerCharacter.h"
#include "Kismet/KismetMathLibrary"
#include "AbilitySystemBlueprintLibrary.h"
APlayerCharacter::APlayerCharacter()
{
// Change this to 1.0f to have it do 100% of remaining health damage.
FallDamage = .9f;
FallDamageMinimumVelocity = 1200.f;
FallDamageMaximumVelocity = 2000.f;
//Cache Tag
LandedTag = FGameplayTag::RequestGameplayTag(FName(TEXT("Combat.State.Fall")));
}
void APlayerCharacter::Landed(const FHitResult& Hit)
{
FGameplayEventData Payload;
const float CurrentVelocity = FMath::Abs(GetVelocity().Z);
const float DamagePercentage = FMath::GetMappedRangeValueClamped(FVector2D(FallDamageMinimumVelocity, FallDamageMaximumVelocity), FVector2D(0.0f, 1.0f), CurrentVelocity) * FallDamage;
const int32 DamageAmount = GetHealth() * DamagePercentage;
//Based on the Above Max damage should be 90% of your current health and should never kill you. Feel free to change at your own leisure.
Payload.EventTag = LandedTag;
Payload.Target = this;
Payload.Instigator = this;
Payload.EventMagnitude = DamageAmount;
UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(this, LandedTag, Payload);
}
#include "MyGame.h"
#include "BaseCharacter.h"
#include "PlayerCharacter.generated.h"
/**
*
*/
UCLASS()
class MyGame_API APlayerCharacter : public ABaseCharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APlayerCharacter();
/*
* CONFIG | FALL DAMAGE
*/
/** Minimum Velocity for Damage to Apply */
UPROPERTY(EditDefaultsOnly, Category = "Config|FallDamage")
float FallDamageMinimumVelocity;
/** Maximum Velocity for Damage to Apply */
UPROPERTY(EditDefaultsOnly, Category = "Config|FallDamage")
float FallDamageMaximumVelocity;
/** How Much Damage to Apply */
UPROPERTY(EditDefaultsOnly, Category = "Config|FallDamage")
float FallDamage;
protected:
void Landed(const FHitResult& Hit) override;
FGameplayTag LandedTag;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment