I am trying to have a ball place a decal on location hit. C++/BPs

I am trying to have this ball hit the floor with a decal attach to it. What is the proper way to use the Spawn Decal Attached function. I had created a material and set it to deferred decal. I set a decal actor in C++ then created the Decal actor in BP. I don’t know the proper way to use this function, could anyone help? I know in BP there is a spawn decal at location function. How do I properly cast to the decal, do I have to attach the decal to a bone etc?

 void ABall::HitFloorWithPaint()
{
	FHitResult Hit;
	FRotator FRotatorRandomDecalRotation;
	FRotator RandomDecalRotation;
	ADecalActor* Decal = Cast<ADecalActor>(GetWorld());
	FRotatorRandomDecalRotation = Hit.ImpactNormal.Rotation();
	RandomDecalRotation.Roll = FMath::FRandRange(-180.0f, 180.0f);
	UGameplayStatics::SpawnDecalAttached(MaterialInstance, Decal->GetDecal()->DecalSize = FVector(20.0f, 20.0f, 20.0f));
	Hit.Component.Get(), Hit.BoneName,
		Hit.ImpactPoint, RandomDecalRotation, EAttachLocation::KeepWorldPosition;
}

This would probably be one of those instances you’d want to use blueprints. Personally i’d make a BlueprintNativeEvent and then use the Spawn Decal At Location node, which makes finding impact nodes easier too but you can make it take in an FVector and just use that. Its a messier solution - sure - but if you just want something to be up and running its really not worth sitting there bashing your head on your keyboard for something thats already prebuilt in the engine

Edit: for this solution you need a reference to a UMaterialInstance and pass that through the function because the spawn decal requires it. Then you’ll need to determain the forward vector - since its a ball spawning this stuff im guessing its on the ground, thatll be getting the up vector and converting it into a rotator which is really simple in bp but im sure its pretty easy in cpp too.

Put a debug line wherever you’re calling the hit ground function. If you’re using on overlap functions or on hit functions you might be running into the same dumb problem im running into. My advice right now though is put a debug line everywhere in the line that has to do with the decal spawning

Also make sure its a material INSTANCE it has to be a material instance or it wont work because the decal systems use material instances only

I even set it as a material instance still it won’t spawn…Wow this is dumb.

I have tried using Blueprints for this and for some reason when the actor hits the ground the material doesn’t spawn I have the material set to deferred decal and the blend mode set to translucent on the material that is why I tried doing in CPP. The blueprints are inside of the actor hitting the floor.
327438-

Here are the decal settings.

It is of type UMaterial how do you set it to UMaterialInstance?

How are you determaining when the ball hits the ground? I think it might be a problem with the collision system and actors that dont move

I am using the event tick.

Even when using event hit the decal won’t spawn.

I even Tried using a boxcollision then setting the decal spawn location to the actor location.

Dog you’re not listening to what im saying. Where ever you have your hit event in the ball - where ever the ball determines that it has hit the ground - must not be working if it isnt spawning. That means theres an issue with the collision that you need to work on.

my guess is you’re having the exact same issue as me. Peep the video on my latest question to see what im talking about. Overlap events dont seem to work if one actor is stationary, in this instance the actor is the floor for you. So that means that overlap events wont work for us regardless of how its handled

Put a debug line during the conditional where its supposed to spawn. If it doesnt fire that means there isnt collision which means you need to redo the collision system

Dude for some frickin’ reason now event hit works I set generate hit events on the floor on the ball and NOW it works.

I had it already set for awhile but now it decided to work this was the weirdest thing.

I was about to come on here with a hacky solution because I discovered that the box collision component magically works but then I saw you put generate collision on the floor. - I did that for my character mesh and my problem was solved too. You’re the MVP!

Thank you very much for the compliment! I am glad my odd question helped!

Here I just use event hit with simulation generated hit events enabled and I spawn a UMaterialInstance Material.

Here is in order to save memory and for posterity.
Here is the C++ code conversion.
>
//.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “BloodDecal”)
UMaterialInstance* BloodDecal;
//.cpp
/Call this in the beginplay./
SphereComponent->OnComponentHit.AddDynamic(this, &ASphere::OnSphereCollHit);
void ASphere::OnSphereCollHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
FVector DecalSize(20.0f, 20.0f, 20.0f);
FRotator RandomDecalRotation = Hit.Location.Rotation();
FVector DecalLocation = Hit.Location;
UGameplayStatics::SpawnDecalAtLocation(//Here is the actor that comes in contact.->OtherActor,BloodDecal,DecalSize,DecalLocation, RandomDecalRotation);
}