Download

Spawn decal depending on condition from server

Hi,

I have a projectile weapon, when it hits a surface given a few conditions the projectile has a random chance to either bounce off or explode. If it does explode, the server calls destroy(). My issue is I want to only spawn a decal if it’s going to explode (i.e. be destroyed). If it’s just bouncing off, I don’t want any decal. However in my OnHit method, the client doesn’t know if the server will decide to call destroy or not so I can’t just chuck the decal code in their.

This is my current OnHit method, where I have the decal (incorrectly) spawning on either condition:


void APXProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    // Spawn splat decal (This is not correct! It will spawn a decal regardless of whether the object is being destroyed or not)
    FRotator RandomDecalRotation = Hit.ImpactNormal.Rotation();
    RandomDecalRotation.Roll = FMath::FRandRange(-180.0f, 180.0f);

    float RandomScale = FMath::FRandRange(15.0f, 22.0f);

    UDecalComponent* PaintSplat = UGameplayStatics::SpawnDecalAtLocation(this, DecalMaterial, FVector(25.0f, RandomScale, RandomScale), Hit.ImpactPoint, RandomDecalRotation, 60.0f);
    PaintSplat->SetFadeScreenSize(0.001f);


    // Destroy projectile
    if (GetLocalRole() == ROLE_Authority)
    {
        float HitDotProduct = -FVector::DotProduct(Hit.ImpactNormal, MovementComp->Velocity.GetSafeNormal());
        if (FMath::FRandRange(0.0f, 1.0f) < HitDotProduct)
        {
            Destroy();
        }
    }
}

Thanks!