Fix damage flash implementation
This commit is contained in:
@@ -111,6 +111,9 @@ void ASpaceshipPawn::BeginPlay()
|
||||
CurrentHealth = MaxHealth;
|
||||
CurrentShield = MaxShield;
|
||||
LastDamageTime = 0.0f;
|
||||
|
||||
// Initialize the dynamic material
|
||||
InitializeDynamicMaterial();
|
||||
}
|
||||
|
||||
void ASpaceshipPawn::Tick(float DeltaTime)
|
||||
@@ -486,41 +489,54 @@ float ASpaceshipPawn::TakeDamage(float DamageAmount, FDamageEvent const& DamageE
|
||||
return DamageToApply;
|
||||
}
|
||||
|
||||
void ASpaceshipPawn::InitializeDynamicMaterial()
|
||||
{
|
||||
if (ShipMesh)
|
||||
{
|
||||
// Store the original material
|
||||
OriginalMaterial = ShipMesh->GetMaterial(0);
|
||||
|
||||
if (OriginalMaterial)
|
||||
{
|
||||
// Create dynamic material instance only once
|
||||
DynamicMaterialInstance = UMaterialInstanceDynamic::Create(OriginalMaterial, this);
|
||||
if (DynamicMaterialInstance)
|
||||
{
|
||||
// Apply the dynamic material instance to the mesh
|
||||
ShipMesh->SetMaterial(0, DynamicMaterialInstance);
|
||||
|
||||
// Initialize flash color to transparent
|
||||
DynamicMaterialInstance->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASpaceshipPawn::ApplyDamageFlash()
|
||||
{
|
||||
// Apply material flash effect to indicate damage
|
||||
if (ShipMesh && ShipMesh->GetMaterial(0))
|
||||
// Only set the flash color if we have a valid dynamic material instance
|
||||
if (DynamicMaterialInstance)
|
||||
{
|
||||
UMaterialInstanceDynamic* DynamicMaterial = UMaterialInstanceDynamic::Create(ShipMesh->GetMaterial(0), this);
|
||||
if (DynamicMaterial)
|
||||
{
|
||||
// Assuming the material has a parameter named "FlashColor"
|
||||
DynamicMaterial->SetVectorParameterValue("FlashColor", DamageFlashColor);
|
||||
ShipMesh->SetMaterial(0, DynamicMaterial);
|
||||
// Set the flash color
|
||||
DynamicMaterialInstance->SetVectorParameterValue("FlashColor", DamageFlashColor);
|
||||
|
||||
// Set timer to reset the flash
|
||||
GetWorldTimerManager().SetTimer(
|
||||
DamageFlashTimerHandle,
|
||||
this,
|
||||
&ASpaceshipPawn::ResetDamageFlash,
|
||||
DamageFlashDuration,
|
||||
false
|
||||
);
|
||||
}
|
||||
// Set timer to reset the flash
|
||||
GetWorldTimerManager().SetTimer(
|
||||
DamageFlashTimerHandle,
|
||||
this,
|
||||
&ASpaceshipPawn::ResetDamageFlash,
|
||||
DamageFlashDuration,
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void ASpaceshipPawn::ResetDamageFlash()
|
||||
{
|
||||
// Reset material flash effect
|
||||
if (ShipMesh && ShipMesh->GetMaterial(0))
|
||||
// Reset the flash color only if we have a valid dynamic material instance
|
||||
if (DynamicMaterialInstance)
|
||||
{
|
||||
UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(ShipMesh->GetMaterial(0));
|
||||
if (DynamicMaterial)
|
||||
{
|
||||
// Reset the flash color (assuming transparent black means no flash)
|
||||
DynamicMaterial->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
}
|
||||
DynamicMaterialInstance->SetVectorParameterValue("FlashColor", FLinearColor(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user