My Characterノードのピンを引っ張り出して今度はgetmaxheと入力してMax Healthノードを選択します。
Healthノードのピンを引っ張って検索窓にdivideと入力してfloat/floatを選択します。
Max Healthノードと除算ノードを接続します。
最終的に以下のようにノードをつなぎ合わせます。
スポンサーリンク
爆弾クラスにダメージを追加する
Explosive.hにダメージ変数を宣言します。
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Damage) float Damage; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Damage) TSubclassOf DamageTypeClass;
Explosive.cppのコンストラクタにDamageのデフォルト値を設定します。
AExplosive::AExplosive()
{
Damage = 50.f;
}
Explosive.cppにApplyDamageを追加します。
void AExplosive::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
if (OtherActor)
{
AMyCharacter* Char = Cast(OtherActor);
if (Char)
{
if (OverlapParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), OverlapParticles, GetActorLocation(), FRotator(0.f), true);
}
if (OverlapSound)
{
UGameplayStatics::PlaySound2D(this, OverlapSound);
}
UAnimInstance* AnimInstance = Char->GetMesh()->GetAnimInstance();
if (AnimInstance && Char->AttackMontage)
{
AnimInstance->Montage_Play(Char->AttackMontage, 1.25);
AnimInstance->Montage_JumpToSection(FName("Damage1"), Char->AttackMontage);
}
UGameplayStatics::ApplyDamage(OtherActor, Damage, nullptr, this, DamageTypeClass);
Destroy();
}
}
}
スポンサーリンク
キャラクターヘルスを減らす
MyCharacter.hにいくつか関数を宣言します。
void DecrementHealth(float Amount); virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser) override;
MyCharacter.cppに関数の定義を記述します。
void AMyCharacter::DecrementHealth(float Amount)
{
Health -= Amount;
}
float AMyCharacter::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser)
{
DecrementHealt(DamageAmount);
return DamageAmount;
}
最後にExplosive_BPを開いて検索窓にDamageと入力、Damage Type ClassにDamage Typeを選択してコンパイルと保存を押します。
今後の課題としては、HPバーがゼロになったらキャラクターが死ぬ設定をする必要があるということです。
スポンサーリンク
スポンサーリンク








コメント