I have a platform I place in my level and is supposed to pulse the emissive glow in a material during Tick(). This all works 100% fine, except for when I have more than one placed in the level at a time. Once I add another platform to my level the pulse on both of them is very erratic. I know I can do this in the material itself which I will probably end up doing. But what is it about my code that the actors are influencing each other? I’d rather understand my issue than just apply a bandaid and run into the problem again later.
.h
UPROPERTY(EditAnywhere, Category = "PlatformSettings")
FLinearColor GlowColor = FLinearColor(1.f, 0.f, 0.f, 1.f);
UPROPERTY(EditAnywhere, Category = "PlatformSettings", meta = (ClampMin = "0.0", ClampMax = "1000.0", UIMin = "0.0", UIMax = "1000.0"))
float GlowIntensity = 225.f;
.cpp
AMainPlatform::AMainPlatform()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
USceneComponent* RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
RootComponent = RootComp;
PlatformMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlatformMesh"));
PlatformMesh->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> PlatformVisualAsset(TEXT("/Game/Discrimmage/StaticMesh/SM_MainPlatform.SM_MainPlatform"));
if (PlatformVisualAsset.Succeeded())
{
PlatformMesh->SetStaticMesh(PlatformVisualAsset.Object);
}
}
void AMainPlatform::OnConstruction(const FTransform& Transform)
{
if (PlatformGlowMatInstance == nullptr)
{
PlatformGlowMatInstance = PlatformMesh->CreateDynamicMaterialInstance(1, PlatformMesh->GetMaterial(1), NAME_None);
}
PlatformGlowMatInstance->SetVectorParameterValue(FName("Color"), GlowColor);
PlatformGlowMatInstance->SetScalarParameterValue(FName("Intensity"), GlowIntensity);
}
// Called every frame
void AMainPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
float GlowPulse = 0.f;
static float PulseDirection;
if (PulseDirection == 0.f)
{
PulseDirection = 1.f;
}
float CurrentIntensity;
PlatformGlowMatInstance->GetScalarParameterValue(FName("Intensity"), CurrentIntensity);
if (CurrentIntensity >= GlowIntensity * 1.5f || CurrentIntensity <= GlowIntensity * 0.5f)
{
PulseDirection *= -1;
}
GlowPulse += DeltaTime * GlowIntensity * PulseDirection;
PlatformGlowMatInstance->SetScalarParameterValue(FName("Intensity"), CurrentIntensity + GlowPulse);
}
Quick video to show the behavior.